I have $fruits_arr:
Array
(
[0] => Array
(
[id] => 213
[fruit] => banana
)
[1] => Array
(
[id] => 438
[fruit] => apple
)
[2] => Array
(
[id] => 154
[fruit] => peach
)
)
And $ids_arr:
Array (
[0] => 213
[1] => 154
)
I want to recreate $fruits_arr to have only array items where id is equal to a value from $ids_arr. I also want to maintain the index/array order of $fruits_arr.
I'm using the following:
$selected_fruits = array();
foreach( $fruits_arr as $fruit ) :
if ( in_array( $fruit['id'], $ids_arr ) ) :
$selected_fruits[] = $fruit;
endif;
endforeach;
print_r( $selected_fruits );
It seems to work but I am wondering if there is a shorter, better way to accomplish this in the latest PHP version.
var_export()foreachmaybe you could usearray_filter