-8

I have two arrays, $array1 and $array2, now I want to take the values from $array2 and put each element at the start of each subArray from $array1

First array:

$array1 = Array (
    [0] => Array (
            [0] => 2
            [1] => 6
            [2] => 15 
            [3] => 6               
    )   
    [1] => Array (
            [0] => 5
            [1] => 8
            [2] => 6
            [3] => 12

    )    
    [2] => Array (
            [0] => 2
            [1] => 5
            [2] => 5
            [3] => 5             
    )    
)

Second array:

$array2 = Array (
    [0] => Outlook
    [1] => Temp
    [2] => Humidity        
)

Expected output (modified/new values bold):

$array1 = Array (
    [0] => Array (
            [0] => 'Outlook'
            [1] => 2
            [2] => 6
            [3] => 15 
            [4] => 6        
    )
    [1] => Array ( 
            [0] => 'Temp'
            [1] => 5
            [2] => 8
            [3] => 6
            [4] => 12         
    )
    [2] => Array (
            [0] => 'Humidity'
            [1] => 2
            [2] => 5
            [3] => 5
            [4] => 5            
    )
)
Rizier123
  • 58,877
  • 16
  • 101
  • 156
ali
  • 19
  • 4
  • 2
    Possible duplicate of [PHP append one array to another (not array\_push or +)](http://stackoverflow.com/questions/4268871/php-append-one-array-to-another-not-array-push-or) – izk Mar 25 '16 at 10:53
  • Where are you stuck in doing this? – Rizier123 Mar 25 '16 at 11:00
  • i cant not understand how i can do this how i merge two array as i want – ali Mar 25 '16 at 11:01
  • You can do this multiple ways. 1) Take a look at [foreach](http://php.net/manual/en/control-structures.foreach.php) and loop through your second array. Plus look at [`array_unshift()`](http://php.net/manual/en/function.array-unshift.php) 2) Or you look at [`array_map()`](http://php.net/manual/en/function.array-map.php) and at `array_unshift()`. Read the pages carefully, look at the examples, try to write some code, and if you get stuck post the attempt here. And we will help you to finish it :) – Rizier123 Mar 25 '16 at 11:05
  • @baboizk Op wants to merge the array differently than described in the dupe. – Rizier123 Mar 25 '16 at 11:09
  • @Rizier123 apologies, you might be right. I did not ask/comment on specific situation like you did. – izk Mar 25 '16 at 11:12

1 Answers1

1

You can use array_walk() with anonymous function and array_unshift():

array_walk
(
    $array1,
    function( &$row, $key, $kind )
    {
        array_unshift( $row, $kind[$key] );
    },
    $array2 
);

eval.in demo

array_walk() modify an array using a custom function. The callable function arguments are the array item (note that we have to set it by reference using &), the array key (optional) and an optional custom parameter (in our case, $array2). Inside the function, with array_unshift()) we can prepend to each item the relative $array2 item, selecting it by key $key.


fusion3k
  • 11,568
  • 4
  • 25
  • 47
  • sir its not worked like i mention in question its give offset error – ali Mar 25 '16 at 11:30
  • just sir i have a minor problem actually above i mention $array2 thats start with index zero actually my $array2 is started index one like bellow $array2 = Array ( [1] => Outlook [2] => Temp [3] => Humidity ) – ali Mar 25 '16 at 11:47
  • sir my $array2 is start from index 1 not 0 .thats why i face offset error kindly guide me – ali Mar 25 '16 at 11:58
  • You can use `$array2 = array_values( $array2 )` to reset keys. – fusion3k Mar 25 '16 at 11:58