[英]How do I get value of specific array element by secondary key?
This has to be easy but I am struggling with it. 这必须很容易,但是我正在努力。 If the array below exists (named "$startersnames") and I specifically want to echo the value that has "qb" as the key, how do I do that?
如果下面的数组存在(名为“ $ startersnames”),并且我特别想回显以“ qb”作为键的值,我该怎么做?
I assumed $startersnames['qb'], but no luck. 我假设$ startersnames ['qb'],但没有运气。
$startersnames[0]['qb'] works, but I won't know that it's index 0. $ startersnames [0] ['qb']有效,但我不知道它的索引为0。
Array
(
[0] => Array
(
[qb] => Tannehill
)
[1] => Array
(
[rb] => Ingram
)
[2] => Array
(
[wr] => Evans
)
[3] => Array
(
[wr] => Hopkins
)
[4] => Array
(
[wr] => Watkins
)
[5] => Array
(
[te] => Graham
)
[6] => Array
(
[pk] => Hauschka
)
[7] => Array
(
[def] => Rams
)
[8] => Array
(
[flex] => Smith
)
)
You can use array_column (from php 5.5) like this: 您可以这样使用array_column (从PHP 5.5开始):
$qb = array_column($startersnames, 'qb');
echo $qb[0];
Demo: http://3v4l.org/QqRuK 演示:http: //3v4l.org/QqRuK
This approach is particularly useful when you need to print all the wr
names, which are more than one. 当您需要打印所有多个
wr
名称时,此方法特别有用。 You can simply iterate like this: 您可以像这样简单地进行迭代:
foreach(array_column($startersnames, 'wr') as $wr) {
echo $wr, "\n";
}
You seem to be expecting an array with text keys and value for each, but the array you have shown is an array of arrays: ie each numeric key has a value which is an array - the key/value pair where you are looking for the key 'qb'. 您似乎期望一个包含文本键和值的数组,但是显示的数组是一个数组数组:即每个数字键都有一个值,该值是一个数组-您在其中查找键/值对。键“ qb”。
If you want to find a value at $array['qb'] then your array would look more like: 如果您想在$ array ['qb']处找到一个值,那么您的数组将更像:
$array = [
'qb' => 'Tannehill',
'rb' => 'etc'
];
now $array['qb'] has a value. 现在$ array ['qb']有一个值。
If the array you are inspecting is a list of key/value pairs, then you have to iterate over the array members and examine each (ie the foreach loop shown in your first answer). 如果要检查的数组是键/值对的列表,则必须遍历数组成员并检查每个数组(即,第一个答案中显示的foreach循环)。
For your multi-dim array, you can loop through the outer array and test the inner array for your key. 对于多维度数组,您可以遍历外部数组并测试内部数组的键。
function findKey(&$arr, $key) {
foreach($arr as $innerArr){
if(isset($innerArr[$key])) {
return $innerArr[$key];
}
}
return ""; // Not found
}
echo findKey($startersnames, "qb");
You can try foreach loop 您可以尝试foreach循环
$key = "qb";
foreach($startersnames as $innerArr){
if(isset($innerArr[$key])) {
echo $innerArr[$key];
}
}
$keyNeeded = 'qb';
$indexNeeded = null;
$valueNeeded = null;
foreach($startersnames as $index => $innerArr){
//Compare key
if(key($innerArray) === $keyNeeded){
//Get value
$valueNeeded = $innerArr[key($innerArray)];
//Store index found
$indexNeeded = $index;
//Ok, I'm found, let's go!
break;
}
}
if(!empty($indexNeeded) && !empty($valueNeeded)){
echo 'This is your value: ';
echo $startersnames[$indexNeeded]['qb'];
echo 'Or: ':
echo $valueNeeded;
}
http://php.net/manual/en/function.key.php http://php.net/manual/en/function.key.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.