1
$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?

2
  • array_keys() supports only single values like this print_r(array_keys($a,"10",true)); Commented Apr 24, 2019 at 9:34
  • 1
    OP seem to know it support single value (as his example that works for single value), his question is why not multi Commented Apr 24, 2019 at 9:54

3 Answers 3

1

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

Comments

0

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;

Comments

0

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){

}

2 Comments

The OP look for the keys not values
use $key variable to get keys

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.