If I was to have three or more arrays like below how could I combine them into a mutlidimensional array?
I’ve tried array_combine but that only combines two arrays:
'menu' =>
array
1 => string '6' (length=1)
4 => string '5' (length=1)
7 => string '3' (length=1)
10 => string '2' (length=1)
13 => string '0' (length=1)
16 => string '0' (length=1)
19 => string '0' (length=1)
22 => string '0' (length=1)
'hidden_menu_id' =>
array
2 => string 'C' (length=1)
5 => string 'C' (length=1)
8 => string 'M' (length=1)
11 => string 'M' (length=1)
14 => string 'M' (length=1)
17 => string 'C' (length=1)
20 => string 'M' (length=1)
23 => string 'C' (length=1)
'hidden_menu_name' =>
array
3 => string 'Category here' (length=13)
6 => string 'Category Four' (length=13)
9 => string 'Times' (length=5)
12 => string 'Guardian' (length=8)
15 => string 'Home' (length=4)
18 => string 'Category Two' (length=12)
21 => string 'Telegraph' (length=9)
24 => string 'Yet Another Category' (length=20)
array_combine creates:
array
'Category here' => string '6' (length=1)
'Category Four' => string '5' (length=1)
'Times' => string '3' (length=1)
'Guardian' => string '2' (length=1)
'Home' => string '0' (length=1)
'Category Two' => string '0' (length=1)
'Telegraph' => string '0' (length=1)
'Yet Another Category' => string '0' (length=1)
How mutlidimensional array do you want? Could you give a sample output? I don’t understand your question clearly.
I -think- what you’re trying to do is…
foreach($input['menu'] AS $key => $value) [
$out[] = array('menu'=>$value,'hidden_menu_id' => $input['hidden_menu_id'][($key + 1)],'hidden_menu_name' => $input['hidden_menu_name'][($key+2)]);
}
Though this would probably better be solved by sorting your initial data better…
If it’s not what you’re looking for, then as stated above, I dont understand what output you want.
I’ve always found this behaviour of array_map quite funky (notice the passing of null as a callback).
<?php
$array = array(
'menu' => array(
1 => 6,
4 => 7,
),
'hidden_menu_name' => array(
2 => 'C',
5 => 'D',
),
'hidden_menu_id' => array(
3 => 'Category One',
6 => 'Category Two'
),
);
$array = array_map(
null,
$array['menu'],
$array['hidden_menu_name'],
$array['hidden_menu_id']
);
print_r($array);
/*
Array
(
[0] => Array
(
[0] => 6
[1] => C
[2] => Category One
)
[1] => Array
(
[0] => 7
[1] => D
[2] => Category Two
)
)
*/
Doesnt preserve the associative names, but a nifty trick to learn
AnthonySterling:
I’ve always found this behaviour of array_map quite funky (notice the passing of null as a callback).
<?php
$array = array(
'menu' => array(
1 => 6,
4 => 7,
),
'hidden_menu_name' => array(
2 => 'C',
5 => 'D',
),
'hidden_menu_id' => array(
3 => 'Category One',
6 => 'Category Two'
),
);
$array = array_map(
null,
$array['menu'],
$array['hidden_menu_name'],
$array['hidden_menu_id']
);
print_r($array);
/*
Array
(
[0] => Array
(
[0] => 6
[1] => C
[2] => Category One
)
[1] => Array
(
[0] => 7
[1] => D
[2] => Category Two
)
)
*/
That’s perfect! just what I needed - yes