tags:

views:

60

answers:

5

I have a value like this:

$id = $_GET['id'];

I need to compare this value to an array which converts this value into another value.

For example, if id==14, then check array for element 14, and return its value, for example in this case "Cars".

Anybody know a simple code for this?

Thanks

+1  A: 

You can do this using array_key_exists:

if (array_key_exists($id, $array)) {
    var_dump($array[$id]);
}

You could also use isset($array[$id]) but it will return false if $array[$id] === null.

Gumbo
`if (isset($array[$id])) { var_dump($array[$id]); }`
nikic
+3  A: 

Maybe:

$val = $arr[$_GET['id']];
krico
`$_GET` will return it as a string, that might be problematic if it's numerically indexed. `$val = $arr[(int) $_GET['id']];`
Core Xii
@Core that is not an issue. `$arr[1]` is the same as `$arr['1']` because you cannot have `array('1' => 'foo', 1 => 'bar')`. The second will overwrite the first.
Gordon
@Gordon: Ok. I like to be explicit about it myself, don't like the implicit conversions in PHP... Like how `"0"` evaluates lazily to `NULL`.
Core Xii
@Core help me for a second please. Under what situations would `"0"` become `NULL`. `var_dump( NULL == "0");` is `FALSE`.
Gordon
@Gordin: Ah, sorry, I meant `false`. Comes up often parsing text and form input, where `"0"` evaluates to `false`.
Core Xii
+2  A: 
$array = array(
  "Books",
  "Furniture",
  .... // 11 more
  "Cars"
 );

 // Remove if indexes already start at 0
 $index = $_GET["id"] - 1; 

 if (array_key_exists($index, $array))
 echo $array[$index];
Pekka
A: 

I'm not sure I entirely follow what you want to do here are some possible solutions:

$_GET['id'] = '14';

 

$ary_of_stuff = array(
1=>'Bike',
2=>'Tricycle',
14=>'Car',
20=>'Plane'
);
$id=$_GET['id'];
$value = $ary_of_stuff[$id];

//$value = 'Car';

 

$id=$_GET['id'];
switch($id){
case 1:
case 2:
   $value='Bike';
   break;
case 14:
   $value='Car';
   break;
}

//$value = 'Car';
BenWells
A: 

Actually, the question is vague in regard to what element 14 means, e.g.

$dataArray = range(1,20);
echo $dataArray[14]; // 15

returns the element with the numeric index 14. Since the array returned by range() is zero-based, it is the 15th element. But when I remove the first element

unset($dataArray[0]);
echo $dataArray[14]; // 15

it is still returning 15 although it is now the really the 14th element. This is because an index does not denote position in the array. I could shuffle the array and index 14 could end up at first position. Or the array could contain associative keys, e.g.

$dataArray = array('foo' => 'bar', 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
echo $dataArray[14]; // 15

and 14 would still return 15. If your $id corresponds to the index number, the solutions given in the various answers so far solve your problem.

If you mean the position of the element in the array, you have to use an ArrayIterator to seek to the (zero-based) position:

$iterator = new ArrayIterator($dataArray);
$iterator->seek(13); // zero-based so 13 is the 14th element
echo $iterator->current(); // 13

or the regular array functions

reset($dataArray);
for($i=1; $i<14;$i++) next($dataArray);
echo current($dataArray); // 13

or somewhat shorter

echo current(array_slice($dataArray, 13, 0)); // 13

because

Pos Index Value
 0: [foo] => bar
 1: [0] => 1
 2: [1] => 2
 3: [2] => 3
 4: [3] => 4
 5: [4] => 5
 6: [5] => 6
 7: [6] => 7
 8: [7] => 8
 9: [8] => 9
10: [9] => 10
11: [10] => 11
12: [11] => 12
13: [12] => 13
14: [13] => 14
15: [14] => 15
Gordon