Jump to content

Merging arrays


slanton

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]

Link to comment
https://forums.phpfreaks.com/topic/12341-merging-arrays/
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.
Link to comment
https://forums.phpfreaks.com/topic/12341-merging-arrays/#findComment-47166
Share on other sites

Archived

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

×
×
  • Create New...

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.