1

I need to check if the value exists in the array and once it exists I need to get the object.

Array
(
    [0] => Array
        (
            [_id] => Array
                (
                    [purok] => test
                    [year] => 2017
                    [options] => below-1
                )

            [data] => Array
                (
                    [58cf4935572d6e32900057ab] => Array
                        (
                            [age-sex-distribution] => Array
                                (
                                    [age-range] => Array
                                        (
                                            [options] => below-1
                                        )

                                    [gender] => Array
                                        (
                                            [male-distribution-count] => 12
                                            [female-distribution-count] => 12
                                        )

                                )

                        )

                )

            [date] => 2017-07-08
        )

    [1] => Array
        (
            [_id] => Array
                (
                    [purok] => test
                    [year] => 2017
                    [options] => toddlers (1-2)
                )

            [data] => Array
                (
                    [58cf4935572d6e32900057ab12] => Array
                        (
                            [age-sex-distribution] => Array
                                (
                                    [age-range] => Array
                                        (
                                            [options] => toddlers (1-2)
                                        )

                                    [gender] => Array
                                        (
                                            [male-distribution-count] => 12
                                            [female-distribution-count] => 12
                                        )

                                )

                        )

                )

            [date] => 2017-07-08
        )

)

I need to check this [options] => below-1 if it exists. One it exist, I need to get the data in the array.

So far, I have tried this one.

$keySearch = "data.options";
$dataOption = array_search("below-1", array_column($rec, $keySearch));
print_r($dataOption);

But got no result.

Thanks for helping me in advance.

3 Answers 3

2
$temp = [];

for ($data as $value){
   if($value['_id']['options'] == 'below-1'){
       $temp[] = $value;
    }
}

print_r($temp);

you can try this

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

function search_array($needle, $haystack) {
     if(in_array($needle, $haystack)) {
          return true;
     }
     foreach($haystack as $element) {
          if(is_array($element) && search_array($needle, $element))
               return true;
     }
   return false;
}

if(!search_array($value, $array)) {
     // do something if the given value does not exist in the array
}else{
     // do something if the given value exists in the array
}

Comments

1

you should try this :

for($i=0; $i < count($rec); $i++) {
     if ($rec[$i]['_id']['options'] === "below-1") {
          $dataOption = $rec[$i]['data'];
          break;
     }
}
print_r($dataOption);

It should do what you expect ;-)

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.