user3758251
user3758251

Reputation: 65

PHP - Get key from another key value through a multi dimensional array

I have the following multidimensional array that is obtained through an 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',
        ),
    );

I would like to obtain the id of USD which is lkjh. I know this can be obtained by simply doing $exchangeID[2]['id']. The problem is that the array is dynamic. For example when it is loaded the first subarray may be EUR instead of GBP, and the third subarray may be KRN instead of USD.

Basically, what I have in mind is to look for the subarray where there is the currency first, then accordingly find the corresponding id. E.g. if I want to find EUR. First I find the EUR, then get 'mnbv'.

I tried this $key = array_search('USD', array_column($exchangeID, 'currency')); but I got the following error in my error_log PHP Fatal error: Call to undefined function array_column() to get at least the array number e.g. in this case 2.

Upvotes: 1

Views: 165

Answers (5)

aimme
aimme

Reputation: 6818

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

Upvotes: 1

aldrin27
aldrin27

Reputation: 3407

Try this if this works:

  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'];
       }
    }
  }

Upvotes: 0

hanshenrik
hanshenrik

Reputation: 21675

<?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);

see working example here http://codepad.viper-7.com/1lg2Gj

Upvotes: 0

Beroza Paul
Beroza Paul

Reputation: 2117

Try this:

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

It is possible to use following custom function to get the 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;
}

Upvotes: 1

Endel Dreyer
Endel Dreyer

Reputation: 1654

You can simply filter your array, like this:

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

You can also return the first element of the filter, using the current method:

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

Upvotes: 2

Related Questions