1
Array
(
    [0] => Array
        (
            [ADADCD] =>       
        )
    [1] => Array
        (
            [ADADCD] => ?     
        )

    [2] => Array
        (
            [ADADCD] => HOSP1 
        )

    [3] => Array
        (
            [ADADCD] => HOSP2 
        )

    [4] => Array
        (
            [ADADCD] => H1    
        )

)

We have an array like this ,I want to search a specific value like HOSP2,What is the process to get the value at index.

NKM
  • 304
  • 2
  • 7
  • 13

4 Answers4

3

Just try with array_search

$key = array_search(array('ADADCD' => 'HOSP1'), $inputArray);
hsz
  • 148,279
  • 62
  • 259
  • 315
3

Loop trough the array and return the index on which you find the value you are looking for.

$searchIndex = -1;
foreach ( $yourArray as $k => $v ) {
  if ( $v['ADADCD'] == 'search value' ) {
    $searchIndex = $k;
    break;
  }
}
Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
0

You can use a combination of foreach() and in_array().

So, first looping through all the indices of the array using foreach().

foreach ($array as $key => $subarray)
   if (in_array($string, $subarray))
       return $key;

So, now for an array like this:

Array
(
    [0] => Array
        (
            [ADADCD] =>       
        )
    [1] => Array
        (
            [ADADCD] => ?     
        )

    [2] => Array
        (
            [ADADCD] => HOSP1 
        )

    [3] => Array
        (
            [ADADCD] => HOSP2 
        )

    [4] => Array
        (
            [ADADCD] => H1    
        )
)

Output

2

Fiddle: http://phpfiddle.org/main/code/t6b-g9r

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
-2
<?php
$array = your array;
$key = array_search('HOSP2', $array);
echo $key;
?>

Output: ADADCD

http://php.net/manual/en/function.array-search.php

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Dino Babu
  • 5,814
  • 3
  • 24
  • 33