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
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
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.