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.