php - Set::combine Cakephp, creating multi dimensional array using single dimensional array -
i have array having multiple values same values array of same keys
eg.
array( [0] => array( 'name' => "bob", 'age' => 20, 'marks'=> 50 ) [1] => array( 'name' => "ryan", 'age' => 20, 'marks'=> 57 ) [2] => array( 'name' => "allen", 'age' => 23, 'marks'=> 70 ) [3] => array( 'name' => "duke", 'age' => 23, 'marks'=> 67 ) [4] => array( 'name' => "matt", 'age' => 24, 'marks'=> 85 ) )
i want convert them according there age following using set::combine in cakephp.
array( [20] => array( [0] => array( 'name' => "bob", 'age' => 20, 'marks'=> 50 ) [1] => array( 'name' => "ryan", 'age' => 20, 'marks'=> 57 ) ) [23] => array( [0] => array( 'name' => "allen", 'age' => 23, 'marks'=> 70 ) [1] => array( 'name' => "duke", 'age' => 23, 'marks'=> 67 ) ) [24] => array( [0] => array( 'name' => "matt", 'age' => 24, 'marks'=> 85 ) ) )
i'm able 1 content particular age.
if understood question correctly, can obtain want using (tested cakephp 3):
hash::combine($array, '{n}.user', '{n}', '{n}.age') ;
you can use array_map
, array_values
want:
array_map('array_values', hash::combine($array, '{n}.name', '{n}', '{n}.age'))
do not forget add following use
statement: use \cake\utility\hash ;
edit: looks using cakephp 2, above should work, need replace hash
set
:
array_map('array_values', set::combine($array, '{n}.name', '{n}', '{n}.age'))
example:
$array = array( 0 => array( 'name' => "bob", 'age' => 20, 'marks'=> 50 ), 1 => array( 'name' => "ryan", 'age' => 20, 'marks'=> 57 ), 2 => array( 'name' => "allen", 'age' => 23, 'marks'=> 70 ), 3 => array( 'name' => "duke", 'age' => 23, 'marks'=> 67 ), 4 => array( 'name' => "matt", 'age' => 24, 'marks'=> 85 ) ) ; debug($array); debug(\cake\utility\hash::combine($array, '{n}.user', '{n}', '{n}.age')); debug(array_map('array_values', \cake\utility\hash::combine($array, '{n}.name', '{n}', '{n}.age')));
output:
\src\controller\pagescontroller.php (line 61) [ (int) 0 => [ 'name' => 'bob', 'age' => (int) 20, 'marks' => (int) 50 ], (int) 1 => [ 'name' => 'ryan', 'age' => (int) 20, 'marks' => (int) 57 ], (int) 2 => [ 'name' => 'allen', 'age' => (int) 23, 'marks' => (int) 70 ], (int) 3 => [ 'name' => 'duke', 'age' => (int) 23, 'marks' => (int) 67 ], (int) 4 => [ 'name' => 'matt', 'age' => (int) 24, 'marks' => (int) 85 ] ] \src\controller\pagescontroller.php (line 62) [ (int) 20 => [ 'bob' => [ 'name' => 'bob', 'age' => (int) 20, 'marks' => (int) 50 ], 'ryan' => [ 'name' => 'ryan', 'age' => (int) 20, 'marks' => (int) 57 ] ], (int) 23 => [ 'allen' => [ 'name' => 'allen', 'age' => (int) 23, 'marks' => (int) 70 ], 'duke' => [ 'name' => 'duke', 'age' => (int) 23, 'marks' => (int) 67 ] ], (int) 24 => [ 'matt' => [ 'name' => 'matt', 'age' => (int) 24, 'marks' => (int) 85 ] ] ] \src\controller\pagescontroller.php (line 63) [ (int) 20 => [ (int) 0 => [ 'name' => 'bob', 'age' => (int) 20, 'marks' => (int) 50 ], (int) 1 => [ 'name' => 'ryan', 'age' => (int) 20, 'marks' => (int) 57 ] ], (int) 23 => [ (int) 0 => [ 'name' => 'allen', 'age' => (int) 23, 'marks' => (int) 70 ], (int) 1 => [ 'name' => 'duke', 'age' => (int) 23, 'marks' => (int) 67 ] ], (int) 24 => [ (int) 0 => [ 'name' => 'matt', 'age' => (int) 24, 'marks' => (int) 85 ] ] ]
Comments
Post a Comment