简体   繁体   中英

If condition to check value of multiple column array php

I am trying to get value of is_activated column key. But can not get the exact value. Have tried array_search()

Review the array

Need to check value of all is_activated key whether it's value is 1 or not.

Array
(
[0] => Array
    (
    [first_name] => Array
            (
                [0] => john 
            )

        [is_activated] => Array
            (
                [0] => 0
            )
    )

[1] => Array
    (

        [first_name] => Array
            (
                [0] => mark
            )
        [is_activated] => Array
            (
                [0] => 1
            )

    )

[2] => Array
    (

        [first_name] => Array
            (
                [0] => pretik
            )
        [is_activated] => Array
            (
                [0] => 0
            )
    )

)

I have tried this below solution but can can not get result.

$is_user_activated = array_search(1,array_column($activity,'is_activated'));

if($is_user_activated == 1) { echo 'yes'; }
else { echo 'no'; }

I think you want to be doing this in a loop rather than using array_search. Use a foreach() to get your desired result:

foreach($yourArray as $item) {
    if($item['is_activated'][0] == 1) {
        echo 'yes';
    } else {
        echo 'no';
    }
}

You can use the array_filter() for such tasks, it allows to use a callback filter function:

<?php
$data = [
    [
        'first_name' => ['john'] ,
        'is_activated' => [0]
    ],
    [
        'first_name' => ['mark'],
        'is_activated' => [1]
    ],
    [
        'first_name' => ['pretik'],
        'is_activated' => [0]
    ]
];
$matches = array_filter($data, function($entry) {
    return in_array(1, $entry['is_activated']);
});
var_dump($matches);

The output of that is:

array(1) {
  [1]=>
  array(2) {
    ["first_name"]=>
    array(1) {
      [0]=>
      string(4) "mark"
    }
    ["is_activated"]=>
    array(1) {
      [0]=>
      int(1)
    }
  }
}

Reason why that is a bit awkward is that your initial data has a very strange structure: the values of the elements are arrays themselves holding the actual values instead of scalar values themselves. That makes searching more complex than in "normal" situations. So take a good look if maybe you can fix that strange structure instead to be able to use an easier search approach.

You can get the activated users via array_filter :

$activated = array_filter($users, function($record) {
    return reset($record['is_activated']) == 1;
});

This will only keep users who are activated, you can then simply count the array to see if you have any activated users:

echo count($activated) ? 'yes' : 'no';

Example here: http://ideone.com/mMuJcO

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