Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Good Morning ya'll,

I am not sure if I got the terms right in my title, but I am trying to do a php array_push like so

array_push($countryList, "US" => "United States");

but this gives me a syntax error.

Am I not doing this properly?

Thanks, J

share|improve this question
 
Please mark the answer if it is posted... –  eL-Prova Mar 13 at 15:00
add comment

2 Answers

up vote 1 down vote accepted

Adding like

$countryList["US"] = "United States";

Pushing a value into an array automatically creates a numeric key for it.

When adding a key-value pair to an array, you already have the key, you don't need one to be created for you. Pushing a key into an array doesn't make sense. You can only set the value of the specific key in the array.

// no key
array_push($array, $value);
// same as:
$array[] = $value;

// key already known
$array[$key] = $value

;

share|improve this answer
add comment

Refer to online php doc ...

"US" => "United States" is not a var !

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.