使用数组值创建字符串

时间:2016-04-11 06:05:03

标签: php codeigniter

我有一个问题集和一个父集,基于父名称我想读问题的answerOption并创建像矩阵一样的字符串。结果将是所有可能性的组合。

我想在结果中写一个字符串,如下所示

key=524136910&CurrentInsuranceStatus=Y&InsuredTimeframe=-1
key=524136910&CurrentInsuranceStatus=Y&InsuredTimeframe=FiveYearsorMore
...
key=524136910&CurrentInsuranceStatus=Parents&InsuredTimeframe=-1
key=524136910&CurrentInsuranceStatus=Parents&InsuredTimeframe=FiveYearsorMore

下面是我的代码,但是它在字符串中重复父节点。 示例CurrentInsuranceStatus这是父级,后=符号是答案

public function createCBQJson($questions,$parents)
{
    $cbqStdObj  = $parents;        
    $jsonString = '';
    $urlArr     = array();

    foreach($cbqStdObj as $key=>$val)
    { 
        if(!in_array($key, array('DriverOccupation')))
        {
            //continue;
        }
        $jsonString = $key;
        if(isset($val->k))
        {
            $cbqKey     = $val->k;
        }
       // echo 'parents of -'.$jsonString.'--<br>';
        $cbqUrl     = '';
        if(isset($val->p) && count($val->p)>=2)
        {
            foreach($val->p as $pkey=>$pval)
            {   
                //create url with parent    
                $answers = $questions[$pval]['QuestionData']->_answerOptions;       
                if(count($answers) > 0)
                {   
                                        foreach($answers  as $ansKey=>$ansValue)
                                        {
                                            $cbqUrl.= '?key='.$cbqKey.'&'.$pval.'='.$ansValue->_value ;           
                                         }                                                          
                }
            }
            $cbqSet[$cbqKey] =  $cbqUrl;                
        }
    }
}

以下是$ questions arrray

Array
(
    [CurrentInsuranceStatus] => Array
        (
            [QuestionData] => Platform_Form_Page_Question Object
                (
                    [_answer] => 
                    [_answerOptions] => Array
                        (
                            [0] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 340280
                                    [_hideAnswer] => 
                                    [_order] => 110
                                    [_promptText] => Yes
                                    [_value] => Y
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                            [1] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 340290
                                    [_hideAnswer] => 
                                    [_order] => 120
                                    [_promptText] => Yes, on parents policy
                                    [_value] => Parents
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                            [2] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 340300
                                    [_hideAnswer] => 
                                    [_order] => 130
                                    [_promptText] => Yes, insured through employer
                                    [_value] => Company
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                            [3] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 340310
                                    [_hideAnswer] => 
                                    [_order] => 140
                                    [_promptText] => No, policy lapsed/expired
                                    [_value] => Lapsed
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                            [4] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 340320
                                    [_hideAnswer] => 
                                    [_order] => 150
                                    [_promptText] => No, never been insured
                                    [_value] => Never
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                            [5] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 340330
                                    [_hideAnswer] => 
                                    [_order] => 160
                                    [_promptText] => No, I didnt have a vehicle to insure
                                    [_value] => First
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                            [6] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 340340
                                    [_hideAnswer] => 
                                    [_order] => 170
                                    [_promptText] => No, been deployed/overseas in the military
                                    [_value] => Military
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                            [7] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 340350
                                    [_hideAnswer] => 
                                    [_order] => 180
                                    [_promptText] => No, other reason
                                    [_value] => Other
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                        )

                    [_answersDivID] => html_CurrentInsuranceStatus
                    [_phoneAreaField] => 
                    [_attributeName] => CurrentInsuranceStatus
                    [_criteriaBasedQuestion] => 1
                    [_dateField] => 
                    [_eventHandler] => 
                    [_fieldType] => select
                    [_hideQuestion] => 1
                    [_monthField] => 
                    [_phoneNumberField] => 
                    [_order] => 10010
                    [_pageAttributes] => stdClass Object
                        (
                            [additionalLabel] => 
                            [answerStyle] => 
                            [colNum] => 
                            [colWidth] => 
                            [cssclass] => 
                            [defaultValue] => 
                            [isMultiSelect] => 
                            [maxLength] => 
                            [min] => 
                            [questionSkinStyle] => 
                            [toolTip] => 
                        )

                    [_phonePreFixField] => 
                    [_questionDivID] => prompt_CurrentInsuranceStatus
                    [_questionKey] => 7520910
                    [_required] => 1
                    [_text] => Current insurance status
                    [_yearField] => 
                    [_errorLog] => Array
                        (
                        )

                )

        )

    [InsuredTimeframe] => Array
        (
            [QuestionData] => Platform_Form_Page_Question Object
                (
                    [_answer] => 
                    [_answerOptions] => Array
                        (
                            [0] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 3063810
                                    [_hideAnswer] => 
                                    [_order] => 100
                                    [_promptText] => Please Select
                                    [_value] => -1
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                            [1] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 653710
                                    [_hideAnswer] => 
                                    [_order] => 110
                                    [_promptText] => 5 years or more
                                    [_value] => FiveYearsorMore
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                            [2] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 653810
                                    [_hideAnswer] => 
                                    [_order] => 120
                                    [_promptText] => 4-5 years
                                    [_value] => FourtoFiveYears
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                            [3] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 654110
                                    [_hideAnswer] => 
                                    [_order] => 130
                                    [_promptText] => 3-4 years
                                    [_value] => ThreetoFourYears
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                            [4] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 654310
                                    [_hideAnswer] => 
                                    [_order] => 140
                                    [_promptText] => 2-3 years
                                    [_value] => TwotoThreeYears
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                            [5] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 654210
                                    [_hideAnswer] => 
                                    [_order] => 150
                                    [_promptText] => 1-2 years
                                    [_value] => TwelveOrMoreMonths
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                            [6] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 654010
                                    [_hideAnswer] => 
                                    [_order] => 160
                                    [_promptText] => 6 to 11 months
                                    [_value] => SixToElevenMonths
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                            [7] => Platform_Form_Page_Question_AnswerOption Object
                                (
                                    [_answerKey] => 653910
                                    [_hideAnswer] => 
                                    [_order] => 170
                                    [_promptText] => Less than 6 months
                                    [_value] => LessThanSixMonths
                                    [_errorLog] => Array
                                        (
                                        )

                                )

                        )

                    [_answersDivID] => html_InsuredTimeframe
                    [_phoneAreaField] => 
                    [_attributeName] => InsuredTimeframe
                    [_criteriaBasedQuestion] => 1
                    [_dateField] => 
                    [_eventHandler] => 
                    [_fieldType] => select
                    [_hideQuestion] => 1
                    [_monthField] => 
                    [_phoneNumberField] => 
                    [_order] => 10020
                    [_pageAttributes] => stdClass Object
                        (
                            [additionalLabel] => 
                            [answerStyle] => 
                            [colNum] => 
                            [colWidth] => 
                            [cssclass] => 
                            [defaultValue] => 
                            [isMultiSelect] => 
                            [maxLength] => 
                            [min] => 
                            [questionSkinStyle] => 
                            [toolTip] => 
                        )

                    [_phonePreFixField] => 
                    [_questionDivID] => prompt_InsuredTimeframe
                    [_questionKey] => 7169110
                    [_required] => 1
                    [_text] => How long have you been continuously insured?
                    [_yearField] => 
                    [_errorLog] => Array
                        (
                        )

                )

        )
)

下面是父数组

stdClass Object
(
    [IncidentClaimAmountPaid] => stdClass Object
        (
            [o] => 13520
            [k] => 524136910
            [p] => Array
                (
                    [0] => CurrentInsuranceStatus,
                    [1] => InsuredTimeframe
                )

            [e] => 
            [r] => 1
            [a] => Array
                (
                    [0] => stdClass Object
                        (
                            [i] => 32
                            [k] => 529582210
                            [p] => Array
                                (
                                    [0] => IncidentClaimLabel
                                )

                            [e] => 
                        )

                    [1] => stdClass Object
                        (
                            [i] => 1
                            [k] => 529580710
                            [p] => Array
                                (
                                    [0] => IncidentClaimLabel
                                )

                            [e] => 
                        )

                    [2] => stdClass Object
                        (
                            [i] => 4
                            [k] => 529581310
                            [p] => Array
                                (
                                    [0] => IncidentClaimLabel
                                )

                            [e] => 
                        )

                    [3] => stdClass Object
                        (
                            [i] => 2
                            [k] => 529581010
                            [p] => Array
                                (
                                    [0] => IncidentClaimLabel
                                )

                            [e] => 
                        )

                    [4] => stdClass Object
                        (
                            [i] => 16
                            [k] => 529581910
                            [p] => Array
                                (
                                    [0] => IncidentClaimLabel
                                )

                            [e] => 
                        )

                    [5] => stdClass Object
                        (
                            [i] => 128
                            [k] => 529582810
                            [p] => Array
                                (
                                    [0] => IncidentClaimLabel
                                )

                            [e] => 
                        )

                    [6] => stdClass Object
                        (
                            [i] => 64
                            [k] => 529582510
                            [p] => Array
                                (
                                    [0] => IncidentClaimLabel
                                )

                            [e] => 
                        )

                    [7] => stdClass Object
                        (
                            [i] => 256
                            [k] => 530551610
                            [p] => Array
                                (
                                    [0] => IncidentClaimLabel
                                )

                            [e] => 
                        )

                    [8] => stdClass Object
                        (
                            [i] => 8
                            [k] => 529581610
                            [p] => Array
                                (
                                    [0] => IncidentClaimLabel
                                )

                            [e] => 
                        )

                )

        )
)

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码来获取输出。

public function ($questions,$parents)
{
    $cbqStdObj  = json_decode($parents);
    $jsonString = '';
    $urlArr     = array();
    $n = 1;
    foreach($cbqStdObj as $key=>$val)
    { 

        $jsonString = $key;
        if(isset($val->k))
        {
            $cbqKey     = $val->k;
        }
        if(!in_array($cbqKey, array('519732810')))
        {
            //continue;
        }
       //echo 'parents of -'.$jsonString.'--<br>';
        $cbqUrl     = '';
        $count = 1;
        $baseUrl = '?key='.$cbqKey;
        if (isset($val->p) && count($val->p) >= 1)
        {                
            $cbqSet = array();
            $newcbqSet = array();

            foreach ($val->p as $pkey => $pval)
            {
                //echo $cbqKey.'=>'.$pval.'<br>';
                //create url with parent    
                $answers = $questions[$pval]['QuestionData']->_answerOptions;
                if (count($answers) > 0)
                {
                    foreach ($answers as $ansKey => $ansValue)
                    {
                        if($count > 1){
                            foreach($cbqSet[$count-1] as $k1=>$url){
                                $cbqUrl = $url.'&' . $pval .'='.$ansValue->_value;
                                $cbqSet[$count][] = $cbqUrl;
                            }
                        }else{
                                $cbqUrl =  $baseUrl .'&' . $pval .'='.$ansValue->_value;
                                $cbqSet[$count][] = $cbqUrl;
                        }
                    }
                    $result = $cbqSet[$count];
                    $count++;
                }     

            }
            if(isset($result) && count($result) > 0){
                $fset[] = array_unique($result);
            }
        } 

    }
     print_r($fset);
}