简体   繁体   中英

How to merge two arrays?

After loop made and using following array I can create a new array.

$data[$val['gid']][$val['rid']][$val['aid']][$teno][$userid]= array();

This results in the array below:

array:1 [
  "FS OTHER" => array:1 [
    "FS OTHER" => array:1 [
      "FS OTHER" => array:1 [
        "FS OTHER" => array:1 [
          "D111" => []
        ]
      ]
    ]
  ]
]

I also have another array:

array:41 [
  0 => array:2 [
    "sid" => "D111"
    "desc1" => "BANGKOK"
  ]
  1 => array:2 [
    "sid" => "D111"
    "desc1" => "NONTHABURI"
  ]
  2 => array:2 [
    "sid" => "D112"
    "desc1" => "PATHUM THANI"
  ]

Now I need to merge this array based on the 'sid' to get the following result:

array:1 [
  "FS OTHER" => array:1 [
    "FS OTHER" => array:1 [
      "FS OTHER" => array:1 [
        "FS OTHER" => array:1 [
          "BANGKOK" => []
          "NONTHABURI"=> []
        ]
      ]
    ]
  ]
]

You can prepare another structure of the second array

$new = [];
foreach ($arr2 as $x) {
    $new[$x['sid']][$x['desc1']] = [];
}
// [D111 => [ BANGKOK => [], NONTHABURI => [] ],..

and then just create the 1st one by:

$data[$val['gid']][$val['rid']][$val['aid']][$teno][$userid]= $new[$userid];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM