douqiao2471 2017-02-02 14:20
浏览 45

如何使用php使用下面的数组创建多维数组

I have the array like given below

Array ( [id] => 1 [businessunit_id] => 1 [service_name] => dropbox [file_path] => /home/john/Desktop/GoogleDrive/alfresco_details.txt [attribute] => App key [value] => wlvii0k1bdm5lk8 )

Array ( [id] => 1 [businessunit_id] => 1 [service_name] => dropbox [file_path] => /home/john/Desktop/GoogleDrive/alfresco_details.txt [attribute] => App secret [value] => eui9d7p62qvxt78 )

Array ( [id] => 1 [businessunit_id] => 1 [service_name] => dropbox [file_path] => /home/john/Desktop/GoogleDrive/alfresco_details.txt [attribute] => Accesstoken [value] => IwSMaEYuDMAAAAAAAAAAC5jf8SyEXQUdf65xCrzFDbqrtlVyPQoJ7OBV1BRyHqqp )

Array ( [id] => 2 [businessunit_id] => 2 [service_name] => s3 [file_path] => /home/john/Desktop/GoogleDrive/alfresco_details.txt [attribute] => AWS_ACCESS_KEY_ID [value] => xxx )

Array ( [id] => 2 [businessunit_id] => 2 [service_name] => s3 [file_path] => /home/john/Desktop/GoogleDrive/alfresco_details.txt [attribute] => AWS_SECRET_ACCESS_KE [value] => xxxxxx )

I want it to convert like below

Array ( [id] => 1 [businessunit_id] => 1 [service_name] => dropbox [file_path] => /home/john/Desktop/GoogleDrive/alfresco_details.txt array([App key] =>wlvii0k1bdm5lk8, [App secret] =>eui9d7p62qvxt78, [Accesstoken] =>IwSMaEYuDMAAAAAAAAAAC5jf8SyEXQUdf65xCrzFDbqrtlVyPQoJ7OBV1BRyHqqp ))

Array ( [id] => 2 [businessunit_id] => 2 [service_name] => s3 [file_path] => /home/john/Desktop/GoogleDrive/alfresco_details.txt array([AWS_ACCESS_KEY_ID] => xxx, [AWS_SECRET_ACCESS_KE] =>xxxxxx) ) 

unable to get the expected result. could you please some one help me out

here is my php code:

while($row = $result->fetch_assoc()) {

    $curid = $row['id'];
    //$pastid = $curid;

        echo '<pre>';
    //print_r($row);

    if($i == 0){

        $newarray[$i]['id'] =  $row['id'];
        $newarray[$i]['businessunit_id'] =  $row['businessunit_id']; 
        $newarray[$i]['file_path'] =  $row['file_path'];
        $newarray[$i]['service_name'] =  $row['service_name'];
        $newarray[$i][$row['attribute']] =  $row['value'];  

    } else {

        if($newarray[$i-1]['id'] == $curid){
            $newarray[$i-1][$row['attribute']] =  $row['value'];
            $newarray[$i]['id'] =  $row['id'];          
        } else{
            $newarray[$i]['id'] =  $row['id'];
            $newarray[$i]['businessunit_id'] =  $row['businessunit_id']; 
            $newarray[$i]['file_path'] =  $row['file_path'];
            $newarray[$i]['service_name'] =  $row['service_name'];
            $newarray[$i][$row['attribute']] =  $row['value'];
        }
    }
    $i++;

}
  • 写回答

2条回答 默认 最新

  • dpkpaxhzffixp8426 2017-02-02 17:08
    关注

    Basically, you're trying to diff the arrays and combine the diff results with the intersect of the three arrays.

    //untested code sample
    function reformatArrays($arrays)
    {
         $diff = [];
         $a = reset($arrays);
         $inter = [];
         foreach($array as $k => $v)
         {
             $diff[] = array_diff($a, $v);
             $inter = array_intersect($a, $v);
             $a = $v;
         }
         return $inter + $diff;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大