0

If I have a array:
$myarray = array("10"=>"apple", "20"=>"banana", etc..) up to 15 values.

and I use $_GET['category'] to get my value from url for instance "10"

How do I echo $myarray to be "apple"?

3
Contributors
2
Replies
18
Views
4 Years
Discussion Span
Last Post by paulkd
0
if(isset($_GET['category'])){
    $cat = $_GET['category'];
    if(isset($myarray[$cat])){
        echo $myarray[$cat];
    }else{
        echo "Not found in array";
    }
}    
0

You can also use array_key_exists

if(array_key_exists('category',$_GET)) {
    if(array_key_exists($_GET['category'], $myarray)) {
        echo $myarray[$_GET['category']];
    } else {
         echo 'not found in array';
    }
}

and shorter and not so pretty.....

if(array_key_exists('category',$_GET)) {
    echo array_key_exists($_GET['category'], $myarray) ? $myarray[$_GET['category']] : 'not found in array';
}

and shorter....and uglier.....

echo (array_key_exists('category',$_GET) AND array_key_exists($_GET['category'], $myarray)) ? $myarray[$_GET['category']] : 'not found in array';
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.