2

seems like i'm blind at the moment. I want to build an empty array(). but instead of getting "0" as array-key i want to have a specific string as the key... Just to make it clear:

$array = array();

gives me:

[0] => array {
}

but i want it like that:

["string"] => array {
}

...this really drives me crazy right now.

Thanks,Alex

1
  • 2
    Your code example is incomplete because an array itself has no "key"... only its elements do. $array['key'] = 'value' is the way to assign keys and values. Commented Feb 5, 2010 at 16:58

3 Answers 3

10
$array = array();
$array['string'] = "foo";  // this makes it so that $array['string'] = "foo"
$array[] = "bar";      // this makes it so that $array[0] = "bar"
$array[] = "barbar";   // this makes it so that $array[1] = "barbar"

print_r($array);

Output:

Array
(
    [string] => foo
    [0] => bar
    [1] => barbar
)

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

5

You mean like this?

$array = array('string' => array());

You might want to refresh your Array knowledge.

Comments

1

$array = array("string" => array())

?

Comments

Your Answer

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.