简体   繁体   English

从具有相同键名的php数组中获取值

[英]Get value from php array that have the same key name

a quick answer about how to get the value (and pass it in a php variable) from an array: 关于如何从数组中获取值(并将其传递到php变量中)的快速解答:

i've this array: 我有这个数组:

$array = array(
            array('name' => 'infotel', 'value' => '+39080123456' ), 
            array('name' => 'location', 'value' => 'Bari')
            );

I need to pass the "value" of "infotel" in a $telephone variable, and the "value" of "location" in a $city variable. 我需要在$ telephone变量中传递“ infotel”的“值”,并在$ city变量中传递“位置”的“值”。

how can i solve this trouble? 我该如何解决这个麻烦? thanks 谢谢

you can create a function for that. 您可以为此创建一个函数。

function custom_search($search_for='',$search_in=array())
{
  if($search_for=='' OR empty($search_in)) return '';
    foreach($search_in as $val)
    {
       if($val['name']==$search_for) {return $val['value'];}
    }
  return '';
}
$telephone=custom_search("infotel",$array);

I might do it using the new array_column() ( requires PHP >= 5.5 ) and array_walk() . 我可以使用新的array_column()需要PHP> = 5.5 )和array_walk()来实现

The good 'ol foreach loop should be fine, or you could just pull them out directly assuming you know what is in the array all the time. 好的'ol foreach循环应该很好,或者假设您一直都知道数组中的内容,则可以直接将它们拉出。 But here's something I think is a little fancier ;) ... 但是,我认为这是一个有点幻想;)...

$arr = array_column($array, 'value', 'name');

array_walk($arr, function(&$v, $k) use (&$telephone, &$city){
    if ($k == 'infotel') $telephone = $v;
    elseif ($k == 'location') $city = $v;
});

echo $telephone; //+39080123456
echo $city;      //Bari

See Demo - updated updated updated 查看演示- 更新更新更新

$telephone='';
$location='';
foreach($array as $k=>$v)
{
   if($v['name']=="infotel") 
   {
     $telephone=$v['value'];
     break;
   }
   if($v['name']=="location") 
   {
     $location=$v['value'];
     break;
   }
}
echo $telephone; 
echo $location;

If you have only this array, use: 如果只有此数组,请使用:

$tel = $array[0]['value'];
$loc = $array[1]['value'];
$array = array(
    array('name' => 'infotel', 'value' => '+39080123456' ),
    array('name' => 'location', 'value' => 'Bari')
);

$telephone = $array[0]['value'];
$city = $array[1]['value'];
echo $telephone;

echo $city;

You can get your desire result by using this code. 您可以使用此代码获得所需的结果。 you need not to worry about array keys. 您不必担心阵列键。 but last key will replace your location and telephone value. 但最后一个键会取代您的位置和电话号码。

$array = array(
            array('name' => 'infotel', 'value' => '+39080123456' ), 
            array('name' => 'location', 'value' => 'Bari')
            );
$key1 = '';
$key2 = '';
foreach($array as $k => $a){
    if(array_search('infotel', $a) != false){
        $key1 = $k;
    }
    if(array_search('location', $a) != false){
        $key2 = $k;
    }
}
echo 'telephone = '.$array[$key1]['value'];
echo 'location = '.$array[$key2]['value'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM