KBD

Keith Devens .com

Thursday, March 18, 2010 Flag waving
The very name "selection" implies that you're choosing between two or more variants. So that means that the end... – Dr. Walter Veith
← Writing About LiteratureNeat software →

Daily link icon Thursday, July 10, 2003

Updated function to merge two PHP arrays

I just noticed a search engine hit for my old PHP function to merge two arrays. Since I've been using updated code in my CMS for a while, I figured I'd at least post the new code here and supercede the old version.

<?php
function merge(&$a, &$b){
    if(
is_array($a) and is_array($b) and !array_key_exists(0$a)){
        
$b_keys array_keys($b);

        
#for each key that exists in both arrays
        
$keys array_intersect(array_keys($a), $b_keys);
        foreach(
$keys as $key){
            
merge($a[$key],$b[$key]);
        }

        
#for every key in the second array that doesn't exist in the first
        
$keys array_diff($b_keys$keys);
        foreach(
$keys as $key){
            
$a[$key] = $b[$key];
        }
    }else{
        
#either $a is a numeric array or $a or $b is a scalar,
        # so copy $b straight over $a
        
$a $b;
    }
}
?>

Update: I realized that (b - a) always equals (b - (a ^ b)), so instead of calculating (b - a), I updated the function to use (b - (a ^ b)) since I had already calculated (a ^ b) in the first step. Sorry... ^ is the closest I can get to union shit, intersection in ASCII.

Update: Here's yet another version. This one's much shorter and simpler. But I'll really have to time all three versions to find out which is the fastest. Though, I can't imagine this version being slower than the original one from the other post, since this one only has one loop and the other one has two.

<?php
function merge(&$a, &$b){
    if(
is_array($a) and is_array($b) and !array_key_exists(0$a)){
        
#if both $a and $b are arrays, and $a isn't a numeric array
        
while(list($key,) = each($b)){
            if(
array_key_exists($key$a)){
                
merge($a[$key], $b[$key]);
            }else{
                
$a[$key] = $b[$key];
            }
        }
        
reset($b);
    }else{
        
#if $a is a numeric array or $a or $b is a scalar
        # overwrite $a with $b
        
$a $b;
    }
}
?>
← Writing About LiteratureNeat software →

Comments XML gif

M. Bean wrote:

hehe... I was reading that as "a to the power of b", in which case I was like "what kind of crack are you smoking?"

∴ M. Bean | 11-Jul-2003 12:04am est | #2391

Adam Langley (http://www.imperialviolet.org) wrote:

Well, http://www.imperialviolet.org/page10.html#e182 suggests that &cap; would do the trick. I'm not sure if you escape ampersands, so that's amp-cap-semicolon if you dont Smiley

AGL

∴ Adam Langley | 11-Jul-2003 5:16am est | http://www.imperialviolet.org | #2394

Keith (http://www.keithdevens.com/) wrote:

Neat! Actually, HTML is escaped automatically, so I don't think I could do that if I wanted to Smiley Something to think about for the next version of my parser though.

Keith | 11-Jul-2003 8:36am est | http://www.keithdevens.com/ | #2395

M. Bean wrote:

Well, let's find out... &cap; is unescaped... and escaped, here it comes... wooo.... my heart is beating so quickly, I can't stand it! \&cap; Did it work?

∴ M. Bean | 11-Jul-2003 10:37am est | #2396

M. Bean wrote:

Oh, no! DENIED.

∴ M. Bean | 11-Jul-2003 10:37am est | #2397

Keith (http://www.keithdevens.com/) wrote:

Told you!

Keith | 11-Jul-2003 1:38pm est | http://www.keithdevens.com/ | #2399

Michael wrote:

I realise this is a very old post, but just to let you know that there is a problem with the last merge function.

$arr1[0] = 15;
$arr1[2] = 30;

$arr2[1] = 45;
$arr2[3] = 60;

merge ($arr1, $arr2);

var_dump ($arr1);

Will delete all the values in $arr1 and replace them with $arr2 due to "!array_key_exists(0, $a)" in the first "if" statement.

∴ Michael | 20-May-2009 2:04am est | #11158

Keith (http://keithdevens.com/) wrote:

Will delete all the values in $arr1 and replace them with $arr2 due to "!array_key_exists(0, $a)" in the first "if" statement.

Working as intended. It's meant to merge associative arrays, not normal arrays, and that's what the check is there for. Yes it's a kludge cause there's no sure way to know whether a PHP array is intended as an associative array or as a normal array.

Keith | 20-May-2009 4:18pm est | http://keithdevens.com/ | #11159

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

March 2010
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

I hate ASP.NET

I hate ASP... I was doing wonders​with PHP, then suddenly one of my​clients...

Johnies: Mar 17, 6:14am

Quantum physics and free will

I knew you were going to say that....

Tom Massey: Mar 15, 9:26pm

Generated in about 0.11s.

(Used 8 db queries)