例如,我有一个数组:
array(
array(
['make']=>ford
['color']=>blue
['type']=>automatic
),
array(
['make']=>chevrolet
['color']=>red
['type']=>manual
)
当我所要做的只是另一个键的值时,是否可以在 PHP 中找到已知键的值?
例如,我有值“blue”,并且我知道它在“color”键中,我现在可以从这些信息中找到“car”的值吗?
已知 key 的已知值是唯一的。 (在此示例中,不能有两个“蓝色”值)
我希望这是有道理的,并提前感谢您的帮助。
最佳答案
$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);
关于php - 查找键的值,其中另一个键的值等于匹配值(关联数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8836465/