charlie
charlie

Reputation: 481

find array item in PHP

I have this array of items:

$tabslist = array('upload' => 'Upload File', 'pending_files' => 'Pending Files', 'call_data' => 'Call Data', 'services_data' => 'Services Data');

and i want to be able to find one of these items, i have tried using:

$title = array_search($_GET["tab"], $tabslist);

the value of $_GET["tab"] is 'upload' so i want $title to be 'Upload File'

Upvotes: 1

Views: 38

Answers (1)

Farnabaz
Farnabaz

Reputation: 4066

simply use isset function

$tabslist = array('upload' => 'Upload File', 'pending_files' => 'Pending Files', 'call_data' => 'Call Data', 'services_data' => 'Services Data');

$title = isset($tablist[$_GET["tab"]]) ? $tablist[$_GET["tab"]] : "";

UPDATE
as Hasse said in comment you can convert $_GET["tab"] to lowercase to insure correctness

$_GET['tab'] = strtolower( $_GET['tab'] );
$title = isset($tablist[$_GET["tab"]]) ? $tablist[$_GET["tab"]] : "";

Upvotes: 2

Related Questions