Re: PHP problem with array keys / pointers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




----- Original Message ----- From: "julian haffegee" <jules@xxxxxxxxxxx>
To: <php-general@xxxxxxxxxxxxx>
Sent: Sunday, December 17, 2006 7:31 PM
Subject:  PHP problem with array keys / pointers


Hi all,

I've a problem thats been bothering me for a week now

I have an array $animals

keys and values like this

1 => cat
2 => dog
3 => mouse
4 => horse

I want to be able to access them using either a key or a pointer, however the key/pointer will be a variable

without variables, if I want dog, I can either use $animals[1] or $animals['2']

but with a variable , eg. $x = 2
$animals[$x] will always give me 'mouse'

what syntax do I need to use for $animals[$x] to give me 'dog' when $x=2 ?

I would think that you have string keys instead of integer ones. Perhaps there is some whitespace in the variable using to set the key?

For example:

$array = array(' 2' => 'dog');  // there is a whitespace before the number 2

will set the element of ordinal number 0 and key ' 2' to 'dog'. To get that value you would either ask for $array[0] or $array[' 2'].

Doing a print_r() or var_dump() on the array will let you know what the actual contents of both the keys and values are.

Satyam

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux