There's nothing wrong with your foreach loop, but you can use a slightly different syntax:
$selected_fruits = [];
foreach ($fruits as $fruit) {
if (in_array($fruit["id"], $selected_ids)) {
$selected_fruits[] = $fruit;
}
}
print_r($selected_fruits);
I've changed $fruits_arr
to $fruits
, and $ids_arr
to $selected_ids
. A variable name should describe the meaning or purpose of the data or resource a variable contains. And you should be consistent about your choices. For instance, why don't the arrays $selected_fruits
and $fruit
have the _arr
post-fix in your code? They are arrays as well.
There's no difference in the way the code above works, when compared with your code, but it is actually shorter than when array_filter()
is used. Shorter code is, however, never a hallmark of better code. I think readability, and simply code that makes sense, are far more important. Using a rather complex function like array_filter()
here is, in my eyes, overkill.
As far as I can tell the syntax, as used in this answer, is used more often than the syntax in your question. For the []
array declaration you need PHP 5.4 or higher.
You also wrote:
I also want to maintain the index/array order of $fruits_arr.
It is not completely clear what this means. If you actually want to maintain the same indexes you have to do a bit more. The code would then look like this:
$selected_fruits = [];
foreach ($fruits as $index => $fruit) {
if (in_array($fruit["id"], $selected_ids)) {
$selected_fruits[$index] = $fruit;
}
}
print_r($selected_fruits);
This would make the indexes of $selected_fruits
equal to those of $fruits_arr
.
It is clear that both foreach
loops, used in the code segments above, will have to walk the whole $fruits
array to find all selected fruits. If the $fruits
array becomes very long, compared to the number of selected fruits, this will not be very efficient. One of the reasons for this is the way that you've defined the $fruits_arr
. The keys in this array don't do much. If I assume that the fruit ids in this array are unique, then it would be very helpful if the $fruits_arr
keys actually were those fruit ids. I can redefine your $fruits_arr
to achieve this, with this code:
$fruits_arr = array_combine(array_column($fruits_arr, "id"), $fruits_arr);
Now the $fruits_arr
looks like this:
Array
(
[213] => Array
(
[id] => 213
[fruit] => banana
)
[438] => Array
(
[id] => 438
[fruit] => apple
)
[154] => Array
(
[id] => 154
[fruit] => peach
)
)
It would, of course, be better to have done this right from the start, so you don't have to manipulate $fruits_arr
to get those useful keys. Given this new array you can now do:
$selected_fruits = [];
foreach ($selected_ids as $fruit_id) {
$selected_fruits[] = $fruits[$fruit_id];
}
print_r($selected_fruits);
This code only walks over the shorter $selected_ids
array. It also doesn't contain the in_array()
function anymore, because I assumed that all selected fruits are present in the fruits array. It wouldn't be a selection otherwise.
Finally, the code above can be shortened to:
$selected_fruits = array_intersect_key($fruits, array_flip($selected_ids));
print_r($selected_fruits);
This is the shortest and most efficient version I can think of.
$fruits_arr
array?in_array()
is not an efficient means to filter your data. \$\endgroup\$