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.

2
  • Always the array index would be 1. If you wanna search multiple array like this, update your question with the definition. Commented Jan 30, 2013 at 8:34
  • duplicate of stackoverflow.com/questions/6661530/… ? Commented Jan 30, 2013 at 8:36

4 Answers 4

3

Just try with array_search

$key = array_search(array('ADADCD' => 'HOSP1'), $inputArray);
Sign up to request clarification or add additional context in comments.

Comments

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;
  }
}

Comments

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

Comments

-2
<?php
$array = your array;
$key = array_search('HOSP2', $array);
echo $key;
?>

Output: ADADCD

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

1 Comment

Please check, what is the output needed by the OP.

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.