我在这里有这个数组:
Array (
[0] => stdClass Object (
[id] => 1
[message] =>
[pinned] => 0
)
[1] => stdClass Object (
[id] => 3
[message] =>
[pinned] => 1
)
)
现在我有一个问题。我需要在此数组中检查此数组中的[固定]键之一是否包含value 1
。
如果可以正确回答,我想做点什么。如果不是,请执行下一步。这是一个尝试,但是不起作用:
if (isset($my_array(isset($my_array->pinned)) === 1) {
//One value of the pinned key is 1
} else {
//No value of the pinned key is 1 -> skip
}
您可以使用array_reduce
:
if (array_reduce($my_array, function ($c, $v) { return $c || $v->pinned == 1; }, false)) {
//One value of the pinned key is 1
} else {
//No value of the pinned key is 1 -> skip
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句