C
C
CrewCut2016-02-01 22:42:23
PHP
CrewCut, 2016-02-01 22:42:23

How to match the key of an associated array with the value of another simple array in PHP?

There are two arrays, for example:

$alpha = Array('ONE' => '70', 'TWO' => '80');
$beta = Array('ONE','THREE');

It is necessary that if the Beta array has a value equal to the Alpha array key, the Alpha value for this key is displayed. Those. in this case, 70 was displayed. There can and should be several values ​​at the output

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wol_fi, 2016-02-01
@CrewCut

$alpha = array('ONE' => '70', 'TWO' => '80');
$beta = array('ONE','THREE');
$result = array_intersect_key($alpha, array_flip($beta));
var_dump($result);

as a result:
array(1) {
  ["ONE"]=>
  string(2) "70"
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question