dtcpvz8162 2016-06-08 19:35
浏览 11
已采纳

在php中格式化数组(3合1)

So currently I have an array that pulls data based on an attribute and it puts the data in its own seperate array. What I need to do is to put these 3 arrays into one, so if one of them is null, it won't give me errors. It should be fairly simply but I can't wrap my head around it.

 //CV eqpValue
 if (is_array(FullDataResponse->dlr->DesignLayoutRecord)) {
    foreach(FullDataResponse->dlr->DesignLayoutRecord as $DesignLayoutRecord_key => $DesignLayoutRecord_value ) {
            if ($DesignLayoutRecord_value->cktEqpOptions->CktEqpOptions && is_array($DesignLayoutRecord_value->cktEqpOptions->CktEqpOptions)) {
                    foreach ($DesignLayoutRecord_value->cktEqpOptions->CktEqpOptions as $cv_obj)
                           {
                                if($cv_obj->attribute === 'CDR')
                                        {
                                             $this->cvCDRList[] = array("cdr" => $cv_obj->eqpValue);
                                        }

                                if($cv_obj->attribute === 'CUSTOMER')
                                        {
                                             $this->cvCustomerList[] = array("customer" => $cv_obj->eqpValue);
                                        }

                            if($cv_obj->attribute === 'LEASE LINE')
                                        {
                                             $this->cvphoneList[] = array("phoneNumber" => $cv_obj->eqpValue);
                                        }  
                                       }
                                    }
                              }
                             }

See how they are currently put into separate arrays like cvCDRList, cvCustomerList, and cvphoneList? How would I put them into a single array? Thanks!!

  • 写回答

2条回答 默认 最新

  • douyinjiao9351 2016-06-08 20:10
    关注

    you can use array_merge.

    <?php
    
       $array1 = array("a12","a12","a13");
       $array2 = array("a21","a22","a23");
       $array3 = array("a31","a32","a33");
    
       $finalArray = array_merge($array1,$array2,$array3);              
       foreach( $finalArray as $key => $value ){
           echo $key."=>".$value."<br>";
       } 
    
    ?>
    

    You can create another property called "allAtributes" . When you finalize to fill your 3 arrays you can call to another method of class than make the merge between 3 array.

    The finally you will have got like this :

    allAtributes (array)=>{ 
            ["cdr"](array)=> {
                ['cdrkey1'] = cdrAtt1 ,
                ['cdrKey2'] = cdrAtt2 ...
            },
            ["customer"](array)=> {
                ['customerkey1'] = customerAtt1 ,
                ['customerKey2'] = customerAtt2 ...
            },
            ["phoneNumber"](array)=> {
                ['phoneNumberkey1'] = phoneNumberAtt1 ,
                ['phoneNumberKey2'] = phoneNumberAtt2 ...
            } 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大