数组PHP数组获得名ID [英] Php Array of arrays get id from name
本文介绍了数组PHP数组获得名ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的PHP以下的数组:
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");
有一个名称,如中文别名,是有可能检索$ my_array各自的ID没有一个循环?
Having a "name" like "othername", is it possible to retrieve the respective "id" from $my_array without a for loop?
推荐答案
您能得到您的阵列列名称
按 array_column
method.Then找到你想要通过 array_search
它会返回数组的索引。
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"));
var_dump($my_array[$index]["id"]);
这篇关于数组PHP数组获得名ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文