I ask those who wish and who are in the know to share ideas on how to implement this. The only thing that my tired brain thought of in the last 15 minutes

$a = array('John', 'Brad'); $b = array('Doe', 'Born'); if(in_array('John', $a)) { foreach($a as $k => $v) { if($v == 'John') { echo $b[$k]; } } } 

and frankly little inspired for successful fast work.

In short, you need to pull the index out of the array $a , transfer it to the array $b and print the result. $b[$k];

  • What do you think the library function will do? same. if you need to effectively search by value, you have a wrong data structure. - VladD
  • Thank you all, I managed with my crutch) somewhat simplified. - Palmervan
  • All by +1 - Palmervan


4 answers 4

array_search ()

returns array index

in_array ()

checks only the presence of a value in the array.

Those. array_search () is right for you

  • array_search () will not return 0 as far as I know - Palmervan
  • one
    If you believe the documentation in case of failure, array_search returns either NULL or FALSE. In any other case, the element index is Yakovlev Andrei.

The key () function should also come up.

  • Would fit if you needed a key. And you need an index) - Palmervan
  • Well, just in the examples associative arrays, so I thought) - rashpil
 $a = array('John', 'Brad'); $b = array('Doe', 'Born'); if (($idx = array_search('John', $a, true)) !== false) { echo isset($b[$idx]) ? $b[$idx] : ''; } 

    If still relevant, here is my crutch:

     $a = array('John', 'Brad'); $b = array('Doe', 'Born'); $flip = array_flip($a); echo (isset($b[$flip['John']])) ? $b[$flip['John']] : '';