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 in PHP by narikkadan
• 10,840 points
8 views

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

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
1 answer

How to delete element by value in array (not key) using php?

Hello @kartik, Using array_search() and unset, try the following: if (($key = ...READ MORE

answered Sep 16, 2020 in PHP by Niroj
• 82,720 points
185 views
0 votes
0 answers

Type hinting - specify an array of objects

Is there a way to specify the ...READ MORE

May 29 in PHP by Kichu
• 19,040 points
10 views
0 votes
0 answers

How to reverse an array in php WITHOUT using the array reverse method

I have an array called reverse array ...READ MORE

Jun 12 in PHP by narikkadan
• 10,840 points
13 views
0 votes
0 answers

How to check whether an array is empty using PHP?

I have an array named players, what ...READ MORE

Jun 20 in PHP by Kithuzzz
• 320 points
3 views
0 votes
1 answer

How to install a specific version of package using Composer?

Hello, Just use php composer.phar require For example : php ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,720 points
2,759 views
0 votes
1 answer

How to merge two arrays while keeping keys instead of reindexing in php?

Hello, Considering that you have $replaced = array('1' => ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,720 points
790 views
0 votes
1 answer

How to check if array is multidimensional or not?

Since the 'second dimension' could be just ...READ MORE

answered Nov 5, 2018 in Others by DataKing99
• 8,240 points
3,921 views
0 votes
1 answer

How to store input value into array then localstorage?

Hello @ abhittac, You have create the array everytime the ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,720 points
4,524 views
0 votes
1 answer

Convert a PHP object to an associative array

Start with simply typecasting the line:- $array = ...READ MORE

answered Feb 23 in PHP by Aditya
• 7,280 points
111 views
0 votes
0 answers

php var_dump() vs print_r()

What is the difference between var_dump() and print_r() in terms of ...READ MORE

May 28 in PHP by Kichu
• 19,040 points
9 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP