-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            
    )
)
6
  • 2
    Possible duplicate of PHP append one array to another (not array_push or +) Commented Mar 25, 2016 at 10:53
  • Where are you stuck in doing this? Commented Mar 25, 2016 at 11:00
  • i cant not understand how i can do this how i merge two array as i want Commented Mar 25, 2016 at 11:01
  • You can do this multiple ways. 1) Take a look at foreach and loop through your second array. Plus look at array_unshift() 2) Or you look at array_map() 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 :) Commented Mar 25, 2016 at 11:05
  • @baboizk Op wants to merge the array differently than described in the dupe. Commented Mar 25, 2016 at 11:09

1 Answer 1

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.


Sign up to request clarification or add additional context in comments.

4 Comments

sir its not worked like i mention in question its give offset error
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 )
sir my $array2 is start from index 1 not 0 .thats why i face offset error kindly guide me
You can use $array2 = array_values( $array2 ) to reset keys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.