kekit kekit - 2 years ago 74
PHP Question

Php Array of arrays get id from name

I have an array like the following in PHP:

$my_array = array();
$my_array[] = array("id"=>"myid1", "name"=>"myname1");
$my_array[] = array("id"=>"otherid", "name"=>"othername");
$my_array[] = array("id"=>"morestuffid", "name"=>"morestuffname");

Having a "name" like "othername", is it possible to retrieve the respective "id" from $my_array without a for loop?

Answer Source

you can get your array column name by array_column method.Then find you want string by array_search its will return the index of array.

$index = array_search("othername",array_column($my_array,"name"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download