Merge 2 nested arrays in one with multiple values

后端 未结 2 358
孤独总比滥情好
孤独总比滥情好 2021-01-28 06:19

I have 2 nested arrays like this

array:1 [
  0 => array:1 [
    \"id\" => 6
  ],
  1 => array:1 [
    \"id\" => 4
  ]
]

array:1 [
  0 => array:1          


        
相关标签:
2条回答
  • 2021-01-28 06:50

    To make it simple, I tried with looping:

    $arr1 = array(
                array("id"=>6),
                array("id"=>"4")
        );
    $arr2 = array(
                array("id"=>509),
                array("id"=>256)
        );
    
    $result = array();
    foreach($arr1 as $k=>$a) {
        $result[$k] = array("ssh_id"=>$a['id'], "d_id" => $arr2[$k]['id']);
    }
    
    0 讨论(0)
  • 2021-01-28 07:00

    You can do this by using for loop also.

    $final = array();
    for($i = 0; $i < count($arr1); $i++){
        $final[] = array("ssh_id" => $arr1[$i]['id'], "d_id" => $arr2[$i]['id']);
    }
    

    Result

    Array
    (
        [0] => Array
            (
                [ssh_id] => 6
                [d_id] => 509
            )
    
        [1] => Array
            (
                [ssh_id] => 4
                [d_id] => 256
            )
    
    )
    
    0 讨论(0)
提交回复
热议问题