查找另一个键的值等于匹配值的键的值(关联数组) [英] Find value of key where value of another key equals matched value (associative array)

查看:95
本文介绍了查找另一个键的值等于匹配值的键的值(关联数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个数组:

array(
    array(
        ['make']=>ford
        ['color']=>blue
        ['type']=>automatic
    ),
    array(
        ['make']=>chevrolet
        ['color']=>red
        ['type']=>manual
)

当我要做的只是另一个键的值时,是否有可能在PHP中找到一个已知键的值?

Is is possible to find in PHP the value of a known key when all I have to go on is the value of another key?

例如,我的值是"blue",并且知道它在"color"键中,现在可以从此信息中查找"car"的值是什么吗?

Say for example, I have the value "blue" and I know that it's in the "color" key, can I now find what the value of "car" is from this information?

已知密钥的已知值是唯一的. (在此示例中,不可能有两个"blue"值)

The known value of the known key is unique. (in this example, there couldn't be two values of "blue")

我希望这是有道理的,在此先感谢您的帮助.

I hope this makes sense, and thanks in advance for your help.

推荐答案

$knownColor = 'blue';
$knownKey = 'color';
$desiredKey = 'make';

foreach ($outerArray as $inner) {
  if ($inner[$knownKey] == $knownColor) {
    $result = $inner[$desiredKey];
    // or to get the whole inner array:
    // $result = $inner;
    break;
  }
}

var_dump($result);

这篇关于查找另一个键的值等于匹配值的键的值(关联数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆