Filter associative array with keys array

hsz

I have following arrays:

$keys

array (size=2)
  0 => string 'foo' (length=3)
  1 => string 'buz' (length=3)

$data

array (size=3)
  'foo' => int 1
  'bar' => int 2
  'buz' => int 3

How to get $data array filtered by $keys values ? Desired output:

array (size=3)
  'foo' => int 1
  'buz' => int 3
Rocket Hazmat

array_intersect_key should be able to help you out here

array_intersect_key($data, array_flip($keys));

The array_flip is needed because array_intersect_key operates on keys, so this makes sure both arrays are in the right format.

DEMO: http://codepad.org/AGpDAZtE

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Displaying keys and values to all elements in an associative array

From Dev

How to get keys of an associative array as an array

From Dev

how to use strings as associative array keys?

From Dev

Split associative array based on keys

From Dev

Filter associative php array

From Dev

How to filter an associative array?

From Dev

Convert array of keys to associative array

From Dev

array_filter converting an indexed array to an associative array

From Dev

Swift: associative array with multiple keys:values

From Dev

Php, how to swap two associative array KEYs?

From Dev

Strip whitespace from associative array Keys

From Dev

Are Array object with keys bad? (mixed associative array style)

From Dev

How to convert a PHP array to an associative array, using index as keys?

From Dev

Return values only (no keys/associative array) in Laravel

From Dev

Create an associative array using another array as keys

From Dev

Modify array keys of an associative array

From Dev

Sorting associative array / javascript object by keys alphabetically

From Dev

Associative array difference with filter

From Dev

Offset printing array keys in an associative object array

From Dev

Associative array with similar keys assignment

From Dev

Split associative array based on keys

From Dev

BASH associative array rearrange keys

From Dev

Associative Array within Associative Array

From Dev

Return values only (no keys/associative array) in Laravel

From Dev

Merge duplicate keys in associative array BASH

From Dev

How to assign new keys to a PHP associative array

From Dev

calling filter on complex dictionary (associative array) in swift

From Dev

Print nested associative array keys as row numbers

From Dev

Compare an associative array where array2 has additional keys

Related Related

  1. 1

    Displaying keys and values to all elements in an associative array

  2. 2

    How to get keys of an associative array as an array

  3. 3

    how to use strings as associative array keys?

  4. 4

    Split associative array based on keys

  5. 5

    Filter associative php array

  6. 6

    How to filter an associative array?

  7. 7

    Convert array of keys to associative array

  8. 8

    array_filter converting an indexed array to an associative array

  9. 9

    Swift: associative array with multiple keys:values

  10. 10

    Php, how to swap two associative array KEYs?

  11. 11

    Strip whitespace from associative array Keys

  12. 12

    Are Array object with keys bad? (mixed associative array style)

  13. 13

    How to convert a PHP array to an associative array, using index as keys?

  14. 14

    Return values only (no keys/associative array) in Laravel

  15. 15

    Create an associative array using another array as keys

  16. 16

    Modify array keys of an associative array

  17. 17

    Sorting associative array / javascript object by keys alphabetically

  18. 18

    Associative array difference with filter

  19. 19

    Offset printing array keys in an associative object array

  20. 20

    Associative array with similar keys assignment

  21. 21

    Split associative array based on keys

  22. 22

    BASH associative array rearrange keys

  23. 23

    Associative Array within Associative Array

  24. 24

    Return values only (no keys/associative array) in Laravel

  25. 25

    Merge duplicate keys in associative array BASH

  26. 26

    How to assign new keys to a PHP associative array

  27. 27

    calling filter on complex dictionary (associative array) in swift

  28. 28

    Print nested associative array keys as row numbers

  29. 29

    Compare an associative array where array2 has additional keys

HotTag

Archive