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?

mickmackusa
49.3k13 gold badges98 silver badges165 bronze badges
asked Jul 9, 2009 at 11:05
1
  • 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. Commented Jul 9, 2009 at 11:11

2 Answers 2

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
79.2k7 gold badges75 silver badges91 bronze badges
answered Jul 9, 2009 at 11:07
Sign up to request clarification or add additional context in comments.

2 Comments

is there a limit on what characters can go inside an array index?
As far as I know there isnt', maybe you can't use double or single quotation (', ") or you have to escape them.
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.

answered Jul 9, 2009 at 12:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.