简体   繁体   中英

Merge two dimensional array in PHP

for a long time can't resolve smth looking like as very simple matter... I want merge a two dimensional arrays. The example:

$arr1 = {
  [532] =
  {
    [0] = "11"
    [1] = "12"
  }
  [273] =
  {
    [0] = "99"
  }
}
$arr2 = {
  [532] =
  {
    [0] = "11"
    [1] = "13"
  }
}

And the result of merging should be, a map on common keys, exactly like that array:

$result = {
  [532] =
  {
     [0] =
     {      
        [0] = "11"
        [1] = "12"
     }
     [1] =
     {
        [0] = "11"
        [1] = "13"
     }
  }
  [273]
     [0] =
     {      
        [0] = "99"
     }
     [1] =
     {

     }
}

I try sometihng like that:

    $result = $arr1;
    foreach ($arr2 as $key => $value) {
        $result[$key] = isset($result[$key]) ? array_merge([$result[$key]], [$value]) : [$value];
    }

But it doesnt work if $arr2 is empty :(

Probably something like this

 $arr1 = {
      [532] =
      {
        [0] = "11"
        [1] = "12"
      }
      [273] =
      {
        [0] = "99"
      }
    }
    $arr2 = {
      [532] =
      {
        [0] = "11"
        [1] = "13"
      }
    }

   $newarray = array();
   foreach ($arr1 as $key => $value) {
      $cu = $arr1[$key];

      $newarray[$key][] = $cu;

      if(!isset($arr2[$key])) {
            $newarray[$key][] = array();
       }
       else {
         $newarray[$key][] = $arr2[$key];
       }
   }

foreach ($arr2 as $key => $value) {
          if(!isset($newarray[$key])) {
             $newarray[$key][] = $arr2[$key]; 
          }
       }

For the second array checking, you need to use isset() either array set or not:

Example:

<?php
$arr1 = array('532'=>array('11','12'),'273'=>array('99'));
$arr2 = array('532'=>array('11','13'));

$newArr = array();
foreach ($arr1 as $key => $value) {
   if(isset($arr2[$key])){
      $newArr[$key][] = $value;
      $newArr[$key][] = $arr2[$key];
   }
   else{
      $newArr[$key] = $value;
   }
}
echo "<pre>";
print_r($newArr);
?>

Result:

Array
(
    [532] => Array
        (
            [0] => Array
                (
                    [0] => 11
                    [1] => 12
                )

            [1] => Array
                (
                    [0] => 11
                    [1] => 13
                )

        )

    [273] => Array
        (
            [0] => 99
        )

)

Further more, if you want to merge both same index than you can use array_merge() some thing like that:

<?php
$arr1 = array('532'=>array('11','12'),'273'=>array('99'));
$arr2 = array('532'=>array('11','13'));

$newArr = array();
foreach ($arr1 as $key => $value) {
   if(isset($arr2[$key])){
      $newArr[$key][] = array_merge($value,$arr2[$key]);      
   }
   else{
      $newArr[$key] = $value;
   }
}
echo "<pre>";
print_r($newArr);
?>

Result:

Array
(
    [532] => Array
        (
            [0] => Array
                (
                    [0] => 11
                    [1] => 12
                    [2] => 11
                    [3] => 13
                )

        )

    [273] => Array
        (
            [0] => 99
        )

)

Note that, ist script , will give you result as you need with unique index. Second script will give you all values in one single array.

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