2

Is there a function which makes:

$array['blue']='Color';

To:

$array['Color']='blue'

And also, is there a limit on what characters can go inside an array index?

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Ali
  • 261,656
  • 265
  • 575
  • 769
  • 2
    Array keys in PHP can be any string of characters. As far as I know, there's no practical length limit. It can be at least several hundred characters long anyways. – Blixt Jul 09 '09 at 11:11

2 Answers2

12

array_flip() exchanges all keys with their associated values in an array. Any characters can be used in the key, however keep in mind that keys must be unique, so:

$array['blue'] = 'Color';
$array['red']  = 'Color';
$array = array_flip($array);

Yields only:

Array
(
    [Color] => red
)
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
usoban
  • 5,428
  • 28
  • 42
  • is there a limit on what characters can go inside an array index? – Ali Jul 09 '09 at 11:11
  • 1
    As far as I know there isnt', maybe you can't use double or single quotation (', ") or you have to escape them. – usoban Jul 09 '09 at 11:12
1

As for the type of characters that can be used as a key, there seems to be no limit (accents, quotes, and other characters are accepted).

As for the size limit, there isn't any either, the only restriction is the script's memory limit (see "What is the max key size for an array in PHP?"

Couldn't find any official PHP documentation mentioning this though.

Community
  • 1
  • 1
altermativ
  • 690
  • 1
  • 6
  • 20