1

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'; }
2
  • 2
    is_activated column's value is array Commented Jun 14, 2017 at 13:59
  • Yes that's why i am stuck over there. Commented Jun 15, 2017 at 6:53

3 Answers 3

3

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';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

7 Comments

Yes the structure is very complected ... is there any way to make it like this way $records = array( array( 'first_name' => 'John', 'last_name' => 'Doe', ), array( 'first_name' => 'Sally', 'last_name' => 'Smith', ), );
And how should i compare your output array value with if condition ; like 'is_activated' value = 1 or not ?
About your first comment: you mean you want to change the structure of the data you already have? Certainly the structure you now here suggests is possible and actually makes much more sense. But I do not know where you get that data from, nor how you prepare it for this search.
About your second comment: obviously you want to replace the condition in_array(1, $entry['is_activated']) with 1==$entry['is_activated'] or simply $entry['is_activated'] by itself.
It is the wordpress data that i got it from wordpress hook get_user_meta()
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.