检查数组PHP中是否存在具有特定值的键

检查数组PHP中是否存在具有特定值的键,php,arrays,Php,Arrays,我在这里看到了这个数组: Array ( [0] => stdClass Object ( [id] => 1 [message] => [pinned] => 0 ) [1] => stdClass Object ( [id] => 3 [message] => [pinned] => 1 ) ) 现在我有个问题。我需要在此数组中检查此数组中的键[pinted]是否包含

我在这里看到了这个数组:

Array (
  [0] => stdClass Object (
     [id] => 1
     [message] =>
     [pinned] => 0
  )
  [1] => stdClass Object (
     [id] => 3
     [message] =>
     [pinned] => 1
  )
)
现在我有个问题。我需要在此数组中检查此数组中的键[pinted]是否包含值
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\u 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
}

通过阵列的基本循环

for($i=0;$i<count($arr);$i++){
  if($arr[$i]->pinned==1){
     // it is pinned - do something

  }else{
     // it isn't pinned - do something else/nothing

  }
}
($i=0;$i计划==1)的
{
//这是别针-做点什么
}否则{
//它没有被钉住-做点别的/什么也不做
}
}

如果您不希望在未固定的情况下执行任何操作,只需完全关闭
else{}
,您将需要迭代数组并单独测试每个对象。您可以使用普通循环或
array\u filter
实现这一点,例如:

$pinnedItems = array_filter($my_array, function($obj) {
  return $obj->pinned === 1;
});
if (count($pinnedItems) > 0) {
  // do something
}
您可以使用$key来查找它。或使用

$key = array_search ('your param', $arr);
找到你需要的东西。

试试这个

使用search()函数查找每个对象中所需的属性,然后检查结果。这是原始代码,它可以写得更好10倍,但这只是为了得到这个想法

<?php
$my_array = [
0 => (object) [
    'id' => 1,
    'message' => 'msg1',
    'pinned' => 0
],
1 => (object) [
    'id' => 3,
    'message' => 'msg3',
    'pinned' => 1
  ],
];

/**
 * Search in array $arrayVet in the attribute $field of each element (object) the value $value
 */
function search($arrayVet, $field, $value) {
    reset($arrayVet);
    while(isset($arrayVet[key($arrayVet)])) {
        if($arrayVet[key($arrayVet)]->$field == $value){
            return key($arrayVet);
        }
        next($arrayVet);
    }
    return -1;
}

$pinnedObject = search($my_array, 'pinned', 1);
if($pinnedObject != -1) {
    //One value of the pinned key is 1
    echo $my_array[$pinnedObject]->message;
} else {
    //No value of the pinned key is 1 -> skip
    echo "not found";
  }
?>

对我来说有点重,但它能工作!也谢谢你的帮助!
<?php
$my_array = [
0 => (object) [
    'id' => 1,
    'message' => 'msg1',
    'pinned' => 0
],
1 => (object) [
    'id' => 3,
    'message' => 'msg3',
    'pinned' => 1
  ],
];

/**
 * Search in array $arrayVet in the attribute $field of each element (object) the value $value
 */
function search($arrayVet, $field, $value) {
    reset($arrayVet);
    while(isset($arrayVet[key($arrayVet)])) {
        if($arrayVet[key($arrayVet)]->$field == $value){
            return key($arrayVet);
        }
        next($arrayVet);
    }
    return -1;
}

$pinnedObject = search($my_array, 'pinned', 1);
if($pinnedObject != -1) {
    //One value of the pinned key is 1
    echo $my_array[$pinnedObject]->message;
} else {
    //No value of the pinned key is 1 -> skip
    echo "not found";
  }
?>