Gunaseelan
Gunaseelan

Reputation: 2542

array_keys function with search value is an array returns empty as result in php (No Foreach)

$full_json ='{
"1stelement": {
"2ndelement": {
  "elements": [
    {
      "test1": 383,
      "test2": 100
    },
    {
      "test1": 48,
      "test2": 5
    },
    {
      "test1": 383,
      "test2": 100
    },
    {
      "test1": 48,
      "test2": 6
    },
    {
      "test1": 383,
      "test2": 100
    },
    {
      "test1": 48,
      "test2": 7
    },
    {
      "test1": 383,
      "test2": 100
    },
    {
      "test1": 48,
      "test2": 8
    },
    {
      "test1": 383,
      "test2": 100
    },
    {
      "test1": 48,
      "test2": 9
    },
    {
      "test1": 383,
      "test2": 100
    },
    {
      "test1": 48,
      "test2": 10
    }
  ]
}
}
}';

$full = json_decode($full_json,true);
$test2range = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
$values=array_keys(array_column($full['1stelement']['2ndelement']['elements'], 'test2'),$test2range ,true);
// $values=array_keys(array_column($full['1stelement']['2ndelement']['elements'], 'test2'),5 ,true); // returns result
var_dump($values);
exit;

I want to get array_keys return values to process further.

Without array_keys function, we can get the values using foreach loop which is not advisable.

As per array_keys definition, search value parameter can be of mixed type, So we can give array of values.

When I tried that, result always give as an empty array which is not an expected result.

Can anyone explain how to accomplish this?

Upvotes: 1

Views: 405

Answers (3)

dWinder
dWinder

Reputation: 11642

As you can see in the array_keys source code there is type comparison before (in function fast_is_identical_function) - this why array not supported - as the core code is not able to break the array.

What the mix_value mean is that you can compare array with array or int with int... - but not multi option of search.

However, you can do like this:

$values = array_keys(array_filter($full['1stelement']['2ndelement']['elements'], function ($e) use ($test2range) {return in_array($e["test2"], $test2range);}));

Or you can implement it yourself:

function array_keys_multi_search($arr, $searchValues, $strict) {
    $res = array();
    foreach($searchValues as $v)
        $res = array_merge($res, array_keys($arr, $v, $strict));
    return $res;
}

Upvotes: 1

PHP Geek
PHP Geek

Reputation: 4033

decode your json array

$full = (array) json_decode($this->input->post($full_json);

then use foreach to get values

 foreach($full['1stelement']['2ndelement']['elements'] as $key => $values){

}

Upvotes: 0

Mayank Dudakiya
Mayank Dudakiya

Reputation: 3869

Try array_intersect() to check matched keys.

$full = json_decode($full_json,true);

$test2range = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);

$values= array_column($full['1stelement']['2ndelement']['elements'], 'test2');

$result=array_intersect($test2range,$values);

var_dump($result);
exit;

Upvotes: 0

Related Questions