I have an array that looks like this:
Array
(
[0] => Array
(
[pizza] => Calzone
[votes] => 1
[id] => 1
)
[1] => Array
(
[pizza] => Margherita
[votes] => 5
[id] => 2
)
[2] => Array
(
[pizza] => Pepperoni
[votes] => 9
[id] => 3
)
[3] => Array
(
[pizza] => Quattro Formaggi
[votes] => 3
[id] => 4
)
[4] => Array
(
[pizza] => Sloppy Giuseppe
[votes] => 7
[id] => 5
)
)
And when I do this:
function getPizza () {
$id = 3;
return array_filter($myArrayAsAbove, function ($arr) use ($id) {
return ($arr['id'] == $id);
});
}
I get this:
[2] => Array
(
[pizza] => Pepperoni
[votes] => 9
[id] => 3
)
Which I’m sure is correct, but really I’d like this:
[0] => Array
(
[pizza] => Pepperoni
[votes] => 9
[id] => 3
)
Notice the 0 instead of the 2.
Could someone help me find a solution and if you’re really nice, explain a little bit about why this is happening, and how it can be fixed – so I’ll know for next time!
array_filterkeeps the array keys. If you don’t want them,array_valuesgives you only the values “wrapped in a new array”: