如何通过二级密钥获取特定数组元素的值?

问题描述:

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?

I assumed $startersnames['qb'], but no luck.

$startersnames[0]['qb'] works, but I won't know that it's index 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
        )

)

这很简单,但我正在努力解决它。 如果下面的数组存在(名为“$ startersnames”)并且我特别想要回显以“qb”为键的值,我该怎么做? p>

我假设$ startersnames ['qb'],但没有运气。 p>

$ startersnames [0] ['qb']有效,但我不知道它是索引0。 p>

  Array 
(
 [0] =>数组
(
 [qb] => Tannehill 
)
 
 [1] =>数组
(  
 [rb] => Ingram 
)
 
 [2] =>数组
(
 [wr] => Evans 
)
 
 [3] =>数组\  n(
 [wr] => Hopkins 
)
 
 [4] =>数组
(
 [wr] => Watkins 
)
 
 [5] => 数组
(
 [te] => Graham 
)
 
 [6] =>数组
(
 [pk] => Hauschka 
)
 
 [7] =  >数组
(
 [def] => Rams 
)
 
 [8] =>数组
(
 [flex] =>史密斯
)
 
)\  ñ代码>  p  re> 
  div>

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

$key = "qb";
foreach($startersnames as $innerArr){
    if(isset($innerArr[$key])) {
        echo $innerArr[$key];
    }
}

You can use array_column (from php 5.5) like this:

$qb = array_column($startersnames, 'qb');
echo $qb[0];

Demo: http://3v4l.org/QqRuK

This approach is particularly useful when you need to print all the wr names, which are more than one. You can simply iterate like this:

foreach(array_column($startersnames, 'wr') as $wr) {
    echo $wr, "
";
}

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: i.e. each numeric key has a value which is an array - the key/value pair where you are looking for the key 'qb'.

If you want to find a value at $array['qb'] then your array would look more like:

$array = [
   'qb' => 'Tannehill',
   'rb' => 'etc'
];

now $array['qb'] has a value.

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 (i.e. the foreach loop shown in your first answer).

$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