php array map on nested -


situation

i have array result returned database call. in example below, fetches many genres can have many books. using join, query pulls books each genre @ same time. here hypothetical result set:

array(     [0] => array (         'id' => 1,         'title' => 'ficton'         'modules' => array(             [0] => array(                 'other_id' => 1                 'other_title' => 'james clavell'             ),             [1] => array(                 'other_id' => 2                 'other_title' => 'terry pratchett'             ),             [2] => array(                 'other_id' => 3                 'other_title' => 'robert ludlum'             ),         ),     [1] => array (         'id' => 2,         'title' => 'non-ficton'         'modules' => array(             [1] => array(                 'other_id' => 5                 'other_title' => 'an excessive book of excessively interesting things'             ),             [2] => array(                 'other_id' => 6                 'other_title' => 'it\'s late, can\'t think of put here'             ),         )     ) ) 

situation

what end array contains modules shown below:

array(     [0] => array(         'other_id' => 1         'other_title' => 'james clavell'     ),     [1] => array(         'other_id' => 2         'other_title' => 'terry pratchett'     ),     [2] => array(         'other_id' => 3         'other_title' => 'robert ludlum'     ),     [3] => array(         'other_id' => 5         'other_title' => 'an excessive book of excessively interesting things'     ),     [4] => array(         'other_id' => 6         'other_title' => 'it\'s late, can\'t think of put here'     ) ) 

problem

now, have no problem achieving through iteration but, feel there better (undiscovered) means achieving this.

question

is shortcut creating desired result. code have far listed below , it's not difficult situation solve. i'm more curious whether there better version of doing following.

ugly code works

here version of code 100% works but, features more iteration care for.

$arytemp = array(); foreach($arygenres $intkey => $arygenre) {     foreach($arygenre['modules'] $arymodule) {         $arytemp[] = $arymodule     } } 

attempt using array map

an attempt using array map , failing horribly

$arytemp = array(); foreach($arygenres $intkey => $arygenre) {     $arytemp[] = array_map(         function($aryrun) { return $aryrun;     },$arygenre['modules']  } 

i love able cut out foreach loop shown above.

if use php 5.5,

$modules = call_user_func_array('array_merge', array_column($arr, 'modules')); 

if not,

$modules = call_user_func_array(     'array_merge',     array_map(         function ($i) {             return $i['modules'];         },         $arr     ) ); 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -