0

I have 2 arrays in PHP, one which simulates properties that can be edited, the other with the properties and the values it should have.

An example is as following:

$arr1 = array(
    'entity1' => array(
        'property1' => null,
        'property2' => null,
    ),
    'entity2' => array(
        'property3' => null,
        'property4' => null,
        'property5' => null,
    )
);
$arr2 = array(
    'entity1' => array(
        'property1' => 1,
        'property2' => 0,
    ),
    'entity2' => array(
        'property3' => 0,
        'property4' => 1,
    )
);

Now I need to merge these arrays and keep the null values, so that the output will be as following:

$output = array(
    'entity1' => array(
        'property1' => 1,
        'property2' => 0,
    ),
    'entity2' => array(
        'property3' => 0,
        'property4' => 1,
        'property5' => null,
    )
);

I tried array_merge() and $arr1 + $arr2 which both output this:

Array
(
    [entity1] => Array
        (
            [property1] => 1
            [property2] => 0
        )

    [entity2] => Array
        (
            [property3] => 0
            [property4] => 1
        )
)

The array can be deeper than displayed in the example, so it would have to be recursive, and the arrays are all associative.

I have read numerous questions on StackOverflow and Googled quite abit, but the answers I found are not recursive or just don't apply to this question.

EDIT

As above explanation for why I asked this question even though I checked other questions with answers is apparently not enough proof or something, I tried the marked duplicated questions answer, which is the following:

$merged = array_merge(array_filter($arr1, 'strval'), array_filter($arr2, 'strval'));

it gave an array to string conversion error cause it is not recursive, which is what I explained.

Jimmy Knoot
  • 2,378
  • 20
  • 29

2 Answers2

2

This recursive function could be a starting point:

function merge_associative($arr1, $arr2) {
  $merged = $arr1;
  foreach ($arr2 as $k => $v) {
    if (is_array($v) && is_array($arr1[$k]))
      $merged[$k] = merge_associative($arr1[$k], $v);
    else
      $merged[$k] = $v;
  }
  return $merged;
}

It will merge associative arrays, but can not handle numeric indices correctly.

rocky3000
  • 1,134
  • 8
  • 9
  • Thanks for your answer, I had made up something at work that I can't remember right now (will post later), but it involved a foreach loop inside the other foreach loop, which is more than you have and is more performance consuming (while you probably won't notice), thanks! – Jimmy Knoot Jul 21 '14 at 20:46
0

If you want to join several arrays just use this function

function array_join() {
    $args = func_get_args();        
    if (count($args) == 1 && isset($args[0], $args[1])) {
        foreach ($args[0] as $value) {
            $param[] = $value;          
        }
    }else{
        $param = $args;         
    }
    
    $array = merge_associative($param[0], $param[1]);
    
    unset($param[0]);
    unset($param[1]);
    
    if (isset($param[2])) $array = array_join($array, $param[2]); 
    
    return $array; 
} 
Community
  • 1
  • 1
Jessé Catrinck
  • 2,227
  • 19
  • 20