php - array merge or array combine?

n00b0101
n00b0101 used Ask the Experts™
on
I'm sure this is easy for someone well-versed in php, but I've made the mistake of overloading my brain, so now I'm really confused as to whether I should use array_combine, array_merge, or something else...  I've been googling and reading php.net for 4 hours and I think I'm just confusing myself even more...  

Essentially, I just want to combine an array while keeping the keys?
    //Here are the original arrays
    [field_sreference] => Array
    	(
    		[0] => Array
    			(
    				[nid] => 28
    			)
    
    		[1] => Array
    			(
    				[nid] => 28
    			)
    
    		[2] => Array
    			(
    				[nid] => 29
    			)
    	)
    
    [field_idelta] => Array
    	(
    		[0] => Array
    			(
    				[value] => 0
    			)
    
    		[1] => Array
    			(
    				[value] => 1
    			)
    
    		[2] => Array
    			(
    				[value] => 0
    			)
    	)
    
    [field_iswitch] => Array
    	(
    		[0] => Array
    			(
    				[value] => 0
    			)
    
    		[1] => Array
    			(
    				[value] => 0
    			)
    
    		[2] => Array
    			(
    				[value] => 0
    			)
    	)
    
    //Here is what I'm trying to achieve:
    
    [combinedarray] => Array
            (
                [0] => Array
                    (
                        [nid] => 28
                        [idelta] => 0
                        [iswitch] => 0
                        
                    )
    
                [1] => Array
                    (
                        [nid] => 28
                        [idelta] => 1
                        [iswitch] => 0
                    )
    
                [2] => Array
                    (
                        [nid] => 29
                        [idelta] => 0
                        [iswitch] => 0
                    )
            )

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
array_combine takes two arrays and then uses one as the keys and the other as the values.
array_merge basically just takes two or more arrays and then just appends the arrays together.
With regard to keys array_merge php manual states that If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
Senior Platform Engineer
Commented:
I don't think array_merge or array_combine are what you are looking for in this case.   I'm unaware of a built in function that does what you want - but here's a function that should work.  It's based on what I found here: http://www.php.net/manual/en/language.operators.array.php#79795

<?php
$arr1 = array(
  array("nid" => 28),
  array("nid" => 29),
  array("nid" => 29)
);

$arr2 = array(
  array("isdelta" => 0),
  array("isdelta" => 1),
  array("isdelta" => 0)
);

$arr3 = array(
  array("isswitch" => 0),
  array("isswitch" => 0),
  array("isswitch" => 0)
);

print_r(array_concatinate($arr1, $arr2, $arr3));

function array_concatinate($a1, $a2) {
  $aRes = $a1;
  $aRays = func_get_args();
  for ($i=1;$i<sizeof($aRays);++$i) {
    $aRay = $aRays[$i];
    foreach ($aRay as $key => $val)
      if (array_key_exists($key, $aRes))
        foreach ($aRay[$key] as $key2 => $val2)
          $aRes[$key][$key2] = $aRay[$key][$key2];
      else
        $aRes[$key] = $aRay[$key];
  }
  return $aRes;
}

?>

Open in new window


Sample output:
[a@a ~/tmp]$ php test.php
Array
(
    [0] => Array
        (
            [nid] => 28
            [isdelta] => 0
            [isswitch] => 0
        )

    [1] => Array
        (
            [nid] => 29
            [isdelta] => 1
            [isswitch] => 0
        )

    [2] => Array
        (
            [nid] => 29
            [isdelta] => 0
            [isswitch] => 0
        )

)

Open in new window

Author

Commented:
thanks!
Most Valuable Expert 2011
Top Expert 2016

Commented:
whether I should use array_combine, array_merge, or something else...
The descriptions are on the PHP web site.  Here is what I might try.
<?php // RAY_temp_noob.php
error_reporting(E_ALL);
echo "<pre>";

// CREATE THE TEST DATA
$fr = array
( array( 'nid' => 28 )
, array( 'nid' => 28 )
, array( 'nid' => 29 )
)
;
$fd = array
( array( 'value' => 0 )
, array( 'value' => 1 )
, array( 'value' => 0 )
)
;
$fi = array
( array( 'value' => 0 )
, array( 'value' => 0 )
, array( 'value' => 0 )
)
;

// COMBINE THE ARRAYS
$combinedarray = array();
while (!empty($fr))
{
    // GET ONE ELEMENT FROM EACH ARRAY
    $x = array_pop(array_shift($fr));
    $y = array_pop(array_shift($fd));
    $z = array_pop(array_shift($fi));

    // COMBINE THE ELEMENTS WITH THE NEW KEYS
    $combinedarray[]
    = array
    ( 'nid'     => $x
    , 'idelta'  => $y
    , 'iswitch' => $z
    )
    ;
}

// SHOW WORK PRODUCT
print_r($combinedarray);

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial