php - How to add keys to an existing multiple array -


i have bellow multiple array:

 array:4 [▼       0 => array:5 [▼         0 => "some content"         1 => "some content"         2 => "some content"         3 => "some content"         4 => "some content"       ]       1 => array:5 [▼         0 => "some content"         1 => "some content"         2 => "some content"         3 => "some content"         4 => "some content"       ]       2 => array:6 [▼         0 => "some content"         1 => "some content"         2 => "some content"         3 => "some content"         4 => "some content"         5 => "some content"       ]       3 => array:5 [▼         0 => "some content"         1 => "some content"         2 => "some content"         3 => "some content"         4 => "some content"       ]     ] 

how can add keys array looks like:

array:4 [▼   0 => array:5 [▼     title => "some content"     subtitle => "some content"     somekey => "some content"     somekey2 => "some content"     somekey3 => "some content"   ]   1 => array:5 [▼     title => "some content"     subtitle => "some content"     somekey => "some content"     somekey2 => "some content"     somekey3 => "some content"   ]   2 => array:6 [▼     title => "some content"     subtitle => "some content"     somekey => "some content"     somekey2 => "some content"     somekey3 => "some content"     somekey4 => "some content"   ]   3 => array:5 [▼     title => "some content"     subtitle => "some content"     somekey => "some content"     somekey2 => "some content"     somekey3 => "some content"   ] ] 

is there way without loop?

there isn't way add keys before because don't know how many arrays create.

thank attention , participation.

i suggest combination of array_walk , array_combine iterate through array , combine preset list of keys values of each nested array.

array_walk — apply user supplied function every member of array
array_combine — creates array using 1 array keys , values

example code

$array=array(   array('some content1','some content','some content','some content','some content'),   array('some content2','some content','some content','some content','some content'),   array('some content3','some content','some content','some content','some content'),   array('some content4','some content','some content','some content','some content') );      $keys=array('title','subtitle','somekey','somekey2','somekey3');  function combine(&$v,$i,$keys) {   $v=array_combine($keys,$v); }  array_walk($array,'combine',$keys);  print_r($array); 

output

array    (     [0] => array         (             [title] => content1             [subtitle] => content             [somekey] => content             [somekey2] => content             [somekey3] => content         )      [1] => array         (             [title] => content2             [subtitle] => content             [somekey] => content             [somekey2] => content             [somekey3] => content         )      [2] => array         (             [title] => content3             [subtitle] => content             [somekey] => content             [somekey2] => content             [somekey3] => content         )      [3] => array         (             [title] => content4             [subtitle] => content             [somekey] => content             [somekey2] => content             [somekey3] => content         )    ) 

here's working example.

note works if nested arrays contain same number of elements , array of key values contains same number of elements (in case five). otherwise, you're php error "both parameters should have equal number of elements".


if prefer, can achieved in 1 line using anonymous function:

array_walk($array,function(&$v,$i,$keys){$v=array_combine($keys,$v);},$keys); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -