1

Lets say I have an array like below

$array = array(
  array( //index 0
    'col_1' => 'one',
    'col_2' => 'two'
  ),
  array( //index 1
    'col_5' => 'five',
    'col_3' => 'three'
  )
);

Now I needed to search for the key value "three" and the output I want to return is 1 (array index). If there is no value exists, I want to return -1.

Actually the flow is,

  • Search for the value, if exists return it's index. In my scenario the index is 1
  • So that I can get $array[1]['col_5']

I found a solution here, but in my scenario, the column names will be differ at any time.

For ex.,

$key = array_search('three', array_column($array, '?'));

In the above code, we need to specify the column name, but in my array, the column names are different.

Deepak M
  • 849
  • 8
  • 17
  • Did you give a try to the first loop provided in the answer? This is certainly what you want – Cid Feb 12 '19 at 13:34
  • the problem I see in your question you are expecting index `3` to become index `1` for some magic reason – Alex Feb 12 '19 at 13:45

3 Answers3

2
function search($array){
for($i=0;$i<count($array);$i++){
    foreach($array[$i] as $key => $value){
    if($value=='three'){
        return $i;;
    }
  }
}

return -1;
}
Twista
  • 241
  • 3
  • 11
1

Not sure what exactly you are looking for, but here is some approach:

https://ideone.com/8S6445

$keys = array_map(
    function ($el) { 
        return array_search('three', $el) === false ? -1 : 1; 
    }, $array);

or probably you wanted something like:

$keys = array_map(
    function ($el) {
        $idx = array_search('three', $el);
        return $idx === false ? -1 : $idx; 
    }, $array);

I hope finally I've got what is your goal:

$keys = array_map(
    function ($el) {
        $idx = array_search('three', $el);
        if ($idx === false) {
            return -1;
        } else {
            return array_search($idx, array_keys($el));
        }
    }, $array);

Even if I understood your goal, I don't think that you really need to get index 1 instead of original key col_3 - in most scenarios col_3 is what you would probably use for the following calculations, and by the way same about transformation false to -1. So my guess the function you need should be just:

$keys = array_map(
  function ($el) {
    return array_search('three', $el); 
  }, $array);

It will return real key for associative array when element is found and false if not.

Or probably I still did not get your goal?

UPDATE If you just need one value do it straight forward:

https://ideone.com/odTCxI

$key = -1;

foreach($array as $k => $el) {
    if (array_search('three', $el) !== false) {
        $key = $k;
        break;
    }
}
echo $key;
Alex
  • 16,739
  • 1
  • 28
  • 51
  • Hi Alex, Thanks for your response. Your code works fine. It returns array as a response. I just need index or -1 as response. – Deepak M Feb 12 '19 at 14:06
1

You can do this using a customized implementation of the array_filter function. However, just using a `foreach``loop would be just as effective.

Example

$array = [
    [
        '1' => 'one',
        '2' => 'two'
    ],
    [
        '1' => 'five',
        '2' => 'three',
    ],
];

// Set our search parameter
$needle = 'three';

$searchResult = current(array_filter(array_keys($array), function ($a) use ($array, $needle) {
    return array_search($needle, $array[$a]);
})) ?: -1;

// Output
var_dump($searchResult);
Peter
  • 8,776
  • 6
  • 62
  • 95