38

I was wondering what is the best way to search keys in an array and return it's value. Something like array_search but for keys. Would a loop be the best way?

Array:

Array([20120425] => 409 [20120426] => 610 [20120427] => 277
      [20120428] => 114 [20120429] => 32 [20120430] => 304
      [20120501] => 828 [20120502] => 803 [20120503] => 276 [20120504] => 162)

Value I am searching for : 20120504

0

5 Answers 5

112

The key is already the ... ehm ... key

echo $array[20120504];

If you are unsure, if the key exists, test for it

$key = 20120504;
$result = isset($array[$key]) ? $array[$key] : null;

Minor addition:

$result = @$array[$key] ?: null;

One may argue, that @ is bad, but keep it serious: This is more readable and straight forward, isn't?

Update: With PHP7 my previous example is possible without the error-silencer

$result = $array[$key] ?? null;
Sign up to request clarification or add additional context in comments.

3 Comments

as I thought, allot of different answers to this. This works great and is the simplest.
The key is already the ... ehm ... key - this is really helpful :) In the heat of the moment one can really forget the obvious. Thanks for saving me time!
Wow... the whole "KISS" thing comes into play here. I was working on this "problem" for an hour now and all the google searches lead to a 1 liner! $result = isset($array[$key]) ? $array[$key] : null;
12
array_search('20120504', array_keys($your_array));

1 Comment

@Matthew: OP did state: "Value I am searching for : 20120504". So it's a safe assumption that he knows what he is looking for.
9
<?php

// Checks if key exists (doesn't care about it's value).
// @link http://php.net/manual/en/function.array-key-exists.php
if (array_key_exists(20120504, $search_array)) {
  echo $search_array[20120504];
}

// Checks against NULL
// @link http://php.net/manual/en/function.isset.php
if (isset($search_array[20120504])) {
  echo $search_array[20120504];
}

// No warning or error if key doesn't exist plus checks for emptiness.
// @link http://php.net/manual/en/function.empty.php
if (!empty($search_array[20120504])) {
  echo $search_array[20120504];
}

?>

Comments

-1

Here is an example straight from PHP.net

$a = array(
    "one" => 1,
    "two" => 2,
    "three" => 3,
    "seventeen" => 17
);

foreach ($a as $k => $v) {
    echo "\$a[$k] => $v.\n";
}

in the foreach you can do a comparison of each key to something that you are looking for

1 Comment

Well I did not provide it since it it 99% there but the code in the foreach statement would look like this foreach ($a as $k => $v) { if($a[$k] ==='20120504'){ echo "\$a[$k] => $v.\n"; } }
-3

For get last value in multidimensional array and order ascending,

    function lastvalue($array, $key)
    {  
        $a = array();
        for($i = 0; $i < count($array); $i++){
                $a[] = $array[$i][$key];
        }
       sort($a);
       return $a[$i-1];
    }

    $a=array(
            array("row" => 2, "column" => 6),
            array("row" => 2, "column" => 3), 
            array("row" => 2, "column" => 7)
           );

    echo lastvalue($a, "column"); //result = 7

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.