搜索数组中的特定值

时间:2019-11-17 11:30:52

标签: php arrays arraylist

嗨,有一个数组保存在变量$someArray中。这是$someArray的内容:

Array ( 
    [0] => Array ( 
        [CityDesc] => Burgbretzingen 
        [CityId] => 63910 
    ) 
    [1] => Array ( 
        [CityDesc] => Bühlerzimmern 
        [CityId] => 1288010 
    ) 
    [2] => Array ( 
        [CityDesc] => Einkorn 
        [CityId] => 320610 
    ) 
    [3] => Array 
        ( 
        [CityDesc] => Scherbenmühle 
        [CityId] => 711910 
        )
    [4] => Array ( 
        [CityDesc] => Schwäbisch Hall 
        [CityId] => 1694510 
    ) 
    [5] => Array ( 
        [CityDesc] => Traubenmühle 
        [CityId] => 493610 
    )
)

如果我搜索63910,我将读出此值"Burgbretzingen"([CityId])。

1 个答案:

答案 0 :(得分:2)

您可以使用array_column

$res = array_column($a, 'CityId', 'CityDesc');
echo empty($res['Burgbretzingen']) ? '' : $res['Burgbretzingen'];

工作示例:-https://3v4l.org/s1TNk