LMSouq
database Open

How to filter an associative array comparing keys with values in an indexed array?

MA
maček
1 month ago
3 views
Problem Description
The callback function in [`array_filter()`][1] only passes in the array's values, not the keys. If I have: $my_array = array("foo" => 1, "hello" => "world"); $allowed = array("foo", "bar"); What's the best way to delete all keys in `$my_array` that are not in the `$allowed` array? Desired output: $my_array = array("foo" => 1); [1]: http://php.net/manual/en/function.array-filter.php

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base