简体   繁体   English

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

[英]Check if there is a key which has a specific value in Array PHP

I've this array here: 我在这里有这个数组:

Array (
  [0] => stdClass Object (
     [id] => 1
     [message] =>
     [pinned] => 0
  )
  [1] => stdClass Object (
     [id] => 3
     [message] =>
     [pinned] => 1
  )
)

Now I've a problem. 现在我有一个问题。 I need to check in this array if one of the keys [pinned] in this array contains the value 1 . 如果此数组中的某个键[pinned]包含值1我需要检查此数组。

If this can be answered true, I want to do something. 如果这可以回答是真的,我想做点什么。 If not, do the next thing. 如果没有,请做下一件事。 This is a try but it's not working: 这是一个尝试,但它不起作用:

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
}

You could use array_reduce : 你可以使用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
}

Demo on 3v4l.org 在3v4l.org上演示

You will need to iterate over the array and test each object individually. 您需要迭代数组并单独测试每个对象。 You can do that with a plain loop or array_filter for example: 你可以使用普通循环或array_filter来做到这array_filter ,例如:

$pinnedItems = array_filter($my_array, function($obj) {
  return $obj->pinned === 1;
});
if (count($pinnedItems) > 0) {
  // do something
}

Basic loop through the array 通过数组的基本循环

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

  }
}

If you wish to do nothing if it isn't pinned, just leave off the else{} completely 如果你没有固定,什么都不做,只需完全取消else{}

    $tmpArray = array(
    array("one", array(1, 2, 3)),
    array("two", array(4, 5, 6)),
    array("three", array(7, 8, 9))
);

foreach ($tmpArray as $inner) {

    if (is_array($inner)) {
        foreach ($inner[1] as $key=>$value) {
           echo $key . PHP_EOL;
        }
    }
}

You can use $key to find it. 您可以使用$ key来查找它。 Or use 或者使用

$key = array_search ('your param', $arr);

to find what you need to. 找到你需要的东西。

Try this. 尝试这个。

Use a search() function to hunt for the desired attribute in each object, then check the result. 使用search()函数在每个对象中搜索所需的属性,然后检查结果。 This is raw code, it could be written 10 times better, but it is just to get the idea. 这是原始代码,它可以写得好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";
  }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM