Looking to construct below multidimensional array. The first level-1 is ok, but the second is set to same level-1.
Question:
How can I direct the second foreach-loop to build the level-2, right under level-1 ?
Wanted result:
Array
(
[id_no_1] => Array
(
[0] => title_1
)
[id_no_2] => Array
(
[0] => title_2
)
)
My attempt:
<?php
// Position [Level-1]
$taxonomy_id = [
"id_no_1",
"id_no_2",
];
// Position [Level-2]
$titles = [
"title_1",
"title_2",
];
$array = [];
// Populate [level-1]
foreach ($taxonomy_id as $key => $value) {
array_push($array, $taxonomy_id["{$key}"]);
}
// Populate [level-2]
foreach ($titles as $key => $value) {
array_push($array, $titles["{$key}"]);
}
print_r($array);
Result
Array
(
[0] => id_no_1
[1] => id_no_2
[2] => title_1
[3] => title_2
)
array_push. You probably want to use pretty much the same syntax here, as was just explained to you under your other recent question, stackoverflow.com/questions/60414697forinstead of aforeachloop might make more sense here, it will allow you to use the loop counter to access the corresponding values from both at the same time.