嗨我有php数组,总是如下所示。我正在使用foreach。所以我想检查值{01}的值{01}是否存在。我的意思是$value['month']
将永远存在,但我想检查的是。它是否存在某种价值。
$value['month']
答案 0 :(得分:1)
使用foreach()
进行迭代并比较:
// whatever the corresponding label should be
$label = '01';
foreach ($data as $key => element) {
if (is_array($element)
&& array_key_exists('label', $element)
&& '01' === $element['label']
) {
// found matching element with $key
}
}
或者,使用array_walk()
进行迭代:
// whatever the corresponding label should be
$label = '01';
array_walk($data, function (array $element) use ($label) {
if (array_key_exists('label', $element) && $label === $element['label']) {
// found matching element
}
});
或者,如果要过滤并查找匹配元素数组,请使用array_filter()
:
// whatever the corresponding label should be
$label = '01';
$matching = array_filter($data, function (array element) use ($label) {
return array_key_exists('label', $element) && $label === $element['label']
});
if (0 !== count($matching)) {
// found at least once in $data
}
供参考,见: