0

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 :(

1
  • What do you mean by if $arr2 is empty. Why to merge if you don't have second array ? Commented Sep 21, 2016 at 9:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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]; 
          }
       }

6 Comments

Arg, your right - think I've got it now - bit tricky without the f5 button!
See where your going with it - ok
Ok, but what if the first array will be empty? I always want to get array with structure like in the example
Then do another foreach loop to stick in any of the ones missing.
I mean.. Arrays may look the other way around
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.