Jump to content


Photo

Merging arrays


  • Please log in to reply
2 replies to this topic

#1 slanton

slanton
  • Members
  • PipPip
  • Member
  • 27 posts
  • LocationNew Zealand

Posted 19 June 2006 - 12:47 AM

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

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

but if the key is numeric it doesn't work as I want
<?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 ) )

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



#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 19 June 2006 - 01:43 AM

Changing these:

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

To

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

*should* do the trick.
~ D Kuang

#3 slanton

slanton
  • Members
  • PipPip
  • Member
  • 27 posts
  • LocationNew Zealand

Posted 19 June 2006 - 01:53 AM

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users