Filter array by its keys using an array of allowed keys

0 votes

The callback function in array_filter() only passes in the array's values, not the keys.

Scenario: If I have-

$my_array = array("foo" => 1, "hello" => "world");

$allowed = array("foo", "bar");

Expected output:

$my_array = array("foo" => 1);

How can I delete all keys in $my_array that are not in the $allowed array?

Jun 20, 2022 in PHP by narikkadan
• 86,360 points
781 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):