I am trying create a multidimensional array but I have the result I want jajaja, this is my code.
All the vars are dynamically created.
foreach ($list_items as $key => $level):
foreach ($list_songs as $k =>$attribute):
$variables[$level][$k] = $attribute; // changed $variables[] to $variables[$level][]
endforeach;
endforeach;
This is for create the muldimensional i have this results when print this:
echo '<pre>' . print_r($list_items,1) . '</pre>';
echo '<pre>' . print_r($list_songs,1) . '</pre>';
echo '<pre>' . print_r($variables,1) . '</pre>';
/*THIS IS THE ARRAY $list_items */
Array
(
[0] => item_name_1
[1] => item_name_2
)
/*THIS IS THE ARRAY $list_songs*/
Array
(
[0] => Musica Ligera
[1] => La chispa adecuada
)
/*THIS IS THE ARRAY $variables*/
Array
(
[item_name_1] => Array
(
[0] => Musica Ligera
[1] => La chispa adecuada
)
[item_name_2] => Array
(
[0] => Musica Ligera
[1] => La chispa adecuada
)
)
I understand the problem, because I am save all the keys for the songs in each key in list items but I don't know how resolve it I want this in the result:
Array
(
[item_name_1] => Array
(
[0] => Musica Ligera
)
[item_name_2] => Array
(
[1] => La chispa adecuada
)
)
All the comments is welcome, regards :D
I think in your case the length of two arrays are same, so use a for loop instead.
for( $i = 0; $i < sizeof($list_items); $i++ ){
$variables[$list_items[$i]][array_keys($list_songs)[$i]] = $list_songs[$i];
}
print_r($variables);
output:
Array
(
[item_name_1] => Array
(
[0] => song1
)
[item_name_2] => Array
(
[1] => song2
)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.