KBD

Keith Devens .com

Friday, January 9, 2009 Flag waving
All warfare is based on deception. – Sun Tzu (The Art of War (I.18))
← 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

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

January 2009
SunMonTueWedThuFriSat
 123
45678910
11121314151617
18192021222324
25262728293031



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

Recent comments XML

The Elegant Universe

Well I have finally found the crazy​guy that preaches useless nonsence​in A...

Joseph Baxter: Jan 7, 11:07pm

I hate Norton Antivirus


SYMANTEC is very​cunning..
Symantec now have a​redeemable cash back offe...

CAN: Jan 4, 6:25pm

Spider solitaire

Hi everyone!

Glad to have found​this site.  I have enjoyed reading​the c...

flwrchld53: Jan 4, 5:30pm

Generated in about 0.244s.

(Used 8 db queries)

mobile phone