$colors = array();
for($i = 1; $i <=2; $i++) {
if ($i == 1) {
$current_colors = array('color1' => 'blue', 'color2' => 'red');
}
else {
$current_colors = array('color3' => 'yellow', 'color4' => 'green');
}
array_push($colors, $current_colors);
}
var_dump($colors);
This script returns:
array(2) { [0]=> array(2) { ["color1"]=> string(4) "blue" ["color2"]=> string(3) "red" } [1]=> array(2) { ["color3"]=> string(6) "yellow" ["color4"]=> string(5) "green" } }
The array that I need shouldn't have any index (in this case 0
and 1
).
Instead of array_push()
I also tried it with array_merge()
but it returns an empty array.
How can I remove the index?