Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,
I have an array of around 20 key items with 13 value in each key. I need to search specific value in all the keys if exists and if it exists I need the key index of that value.
For e.g. I have
Array
(
[0] => Array
(
[0] => abc
[1] => def
[2] => ghi
[3] => jkl
[4] => mno
[5] => dfdsg
[6] => PIC
[7] => pqr
[8] => 1543
[9] => stu
[10] => wx
[11] => yz
[12] => we
[13] => fewfw
)
)
From this I need to search for "def" and I need the key index of "def".
Please help .

What I have tried:

$ids = array_column($array, '1');
if(isset($ids[$array[$ind][1]]))
{
}
Posted
Updated 3-Mar-17 1:14am

1 solution

Try this:
<?php 

$array = array(array("a", "def", "c"),array("x", "y", "z"),array("x", "y", "def")); 

$keys = [];

for ($i=0; $i<count($array); $i++) {
    if (in_array("def", $array[$i])) {
        array_push($keys, $i);
    }
}

print_r($keys);  
    
?>
or demo[^]
 
Share this answer
 
v2
Comments
User1454 5-Mar-17 0:35am    
Hi PeterLeow, it works well.thanks
Peter Leow 5-Mar-17 0:45am    
You are welcome and remember to mark this as answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900