0

I have tried the following:

$myArray = array();
array_push($myArray,"A"=>array("x","y"));

I get prompted with this error:

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW)

  • Is the above possible to do in PHP?

  • Am I doing something wrong?

  • Is there a more conventional way of doing this?

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Josh Boiskin
  • 69
  • 1
  • 10

1 Answers1

0

I've never liked array_push() and never use it. That's invalid syntax and you can't use array_push() to specify a key. To specify a key:

$myArray["A"] = array("x","y");

Which will yield:

(
    [A] => Array
        (
            [0] => x
            [1] => y
        )
)

Unless you really want an array like this:

(
    [0] => Array
        (
            [A] => Array
                (
                    [0] => x
                    [1] => y
                )
        )
)

Then you would do:

array_push($myArray, array("A" => array("x","y")));

But that's probably not what you want. I always go with $array[] or $array['key'] syntax.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87