Jump to content

Archived

This topic is now archived and is closed to further replies.

slanton

Merging arrays

Recommended Posts

I want to merge two arrays and the following works as I want

[code]
<?php
                $book1=array(alphabet => array('a','b','c'));            
    $book2=array(alphabet => array('d','e','f'));
    $book3 = array_merge_recursive($book1, $book2);
    print_r($book3);    
            ?>
//returns
Array ( [alphabet] => Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f ) )

[/code]
but if the key is numeric it doesn't work as I want
[code]
<?php
                $book1=array(101 => array('a','b','c'));            
    $book2=array(101 => array('d','e','f'));
    $book3 = array_merge_recursive($book1, $book2);
    print_r($book3);    
            ?>
//returns
Array ( [0] => Array ( [0] => a [1] => b [2] => c ) [1] => Array ( [0] => d [1] => e [2] => f ) )

[/code]
How can I get the numeric merge to return
[code]
Array ( [101] => Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f ) )
[/code]

Share this post


Link to post
Share on other sites
Changing these:

[code]     $book1=array(101 => array('a','b','c'));            
    $book2=array(101 => array('d','e','f'));[/code]

To

[code]     $book1=array('101' => array('a','b','c'));            
    $book2=array('101' => array('d','e','f'));[/code]

*should* do the trick.

Share this post


Link to post
Share on other sites
Yes I had tried that as well because the recursive merge only works that way with strings but it didn't work when I tried that which puzzles me. Shouldn't putting the numbers between '' make them into a string?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.