简体   繁体   中英

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 .

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 :

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

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:

$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

    $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. 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. This is raw code, it could be written 10 times better, but it is just to get the idea.

<?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";
  }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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