I need to combine two arrays

Discussion in 'PHP' started by meroving, Jul 4, 2009.

  1. #1
    when I try the php array_merge functions the arrays seem to be overwriting each other.

    Here are two of the example arrays:

    
    
    Array
    (
        [1] => Array
            (
                [Product] => Array
                    (
                        [detail_url] => product32.php
                    )
    
            )
    
        [2] => Array
            (
                [Product] => Array
                    (
                        [detail_url] => product12.php
                    )
    
            )
    )
    
    
    Code (markup):
    and

    
    
    Array
    (
        [1] => Array
            (
                [Product] => Array
                    (
                        [name] => Product 41
                    )
    
            )
    
        [2] => Array
            (
                [Product] => Array
                    (
                        [name] => Product 45
                    )
    
            )
    )
    
    
    Code (markup):

    it need to read ->

    
    
     Array
    (
        [1] => Array
            (
                [Product] => Array
                    (
                        [detail_url] => product32.php
                        [name] => Product 41
                    )
    
            )
    
        [2] => Array
            (
                [Product] => Array
                    (
                        [detail_url] => product12.php
                        [name] => Product 45
                    )
    
            )
    )
    
    
    
    Code (markup):
    does anyone know how to combine these two arrays?
     
    meroving, Jul 4, 2009 IP
  2. Dox5

    Dox5 Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could do it with a loop:

    
    $i = 1;
    foreach ($Array1 as $val)
    {
    	$Array3($i,1) = $Array1($i);
    	$Array3($i,2) = $Array2($i);
    	$i = $i + 1;
    }
    //Array3 is then the final array 
    
    PHP:
    unless thats an array within an array, then it needs a little altering
     
    Dox5, Jul 5, 2009 IP