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条)

报告相同问题?

悬赏问题

  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!