PHP - 通过多维数组从另一个键值获取键

时间:2015-09-05 23:29:10

标签: php arrays

我有以下通过API获得的多维数组。

$exchangeID = array(
    0 => array(
        'id' => 'vcxz', 
        'currency' => 'GBP',
        ),
    1 => array(
        'id' => 'mnbv',
        'currency' => 'EUR',
        ),
    2 => array(
        'id' => 'lkjh',
        'currency' => 'USD', 
        ),
    3 => array(
        'id' => 'poiuy',
        'currency' => 'KRN',
        ),
    );

我想获取美元的 id ,即 lkjh 。我知道这可以通过简单地做$ exchangeID [2] ['id']来获得。问题是阵列是动态的。例如,当它被加载时,第一个子阵列可能是EUR而不是GBP,第三个子阵列可能是KRN而不是USD。

基本上,我想到的是首先找到有货币的子阵列,然后找到相应的ID。例如。如果我想找到欧元。首先我找到欧元,然后得到'mnbv'。

我尝试了这个$key = array_search('USD', array_column($exchangeID, 'currency'));,但我在error_log PHP Fatal error: Call to undefined function array_column()中收到以下错误,以获取至少数组编号,例如在这种情况下2。

5 个答案:

答案 0 :(得分:2)

您可以简单地过滤您的数组,如下所示:

$usd_exchanges = array_filter($exchangeID, function($row) {
  return $row['currency'] == "USD";
}));
var_dump($usd_exchanges[0]);

您还可以使用current方法返回过滤器的第一个元素:

$usd_exchange = current(array_filter($exchangeID, function($row) {
  return $row['currency'] == "USD";
})));
var_dump($usd_exchange);

答案 1 :(得分:1)

foreach($exchangeID as $key=>$value)
    {
        if($exchangeID[$key]['currency']=='USD'){
            $usdId=$exchangeID[$key]['id'];
      break;
        }
    }
//echo $usdId; 
//Result:ikjh

答案 2 :(得分:1)

试试这个:

foreach ($exchangeID as $key => $arr)
  if($arr['currency'] == 'USD')
    echo $key;

可以使用以下自定义函数来获取密钥:

echo getKeyRecursive('USD', $exchangeID);
function getKeyRecursive($needle, $haystack, $strict = false)
{
  foreach ($haystack as $key => $item){
     if(($strict ? $item === $needle : $item == $needle) || (is_array($item) && getKeyRecursive($needle, $item, $strict))) return $key;
  }
  return false;
}

答案 3 :(得分:0)

<?php
$exchangeID = array(
    0 => array(
        'id' => 'vcxz', 
        'currency' => 'GBP',
        ),
    1 => array(
        'id' => 'mnbv',
        'currency' => 'EUR',
        ),
    2 => array(
        'id' => 'lkjh',
        'currency' => 'USD', 
        ),
    3 => array(
        'id' => 'poiuy',
        'currency' => 'KRN',
        ),
    );
$db=new PDO('sqlite::memory:','','',array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$db->query('CREATE TABLE `troll` (`dbid` TEXT, `dbcurrency` TEXT);');
$stm=$db->prepare("INSERT INTO `troll` (`dbid`,`dbcurrency`) VALUES(:id,:currency);");
array_walk($exchangeID,function($arr)use($stm){$stm->execute($arr);});
$res=$db->query("SELECT `dbid` AS `id` FROM `troll` WHERE `dbcurrency` = ".$db->quote('USD').' LIMIT 1');

$id=$res->fetch(PDO::FETCH_ASSOC);
$id=$id['id'];
var_dump($id);

请参阅此处的工作示例http://codepad.viper-7.com/1lg2Gj

答案 4 :(得分:0)

如果有效,请尝试此操作:

  foreach($exchangeID as $key => $val)
  {
      if(array_key_exists('currency', $val))
        if($val['currency'] == 'USD'){
          echo $val['id'];
          echo $val['currency'];
        elseif($val['currency'] == 'GBP'){a1
           echo $val['id'];
          echo $val['currency']);
       elseif($val['currency'] == 'EUR'){
          echo $val['id'];
         echo $val['currency'];
         }else{
          echo $val['id'];
         echo $val['currency'];
       }
    }
  }