Array push with associate array Array push with associate array arrays arrays

Array push with associate array


You are using an associative array so you just set the key/value pair like this.

$array["Password"] = pass;

I think you may need to review the difference between an array and an associative array. For example if I ran the same command again with a different value it would overwrite the old one:

$array["Password"] = "overwritten";

Giving you this

Array ( [Username] => user         [Email] => email        [Password] => "overwritten"      )

Which judging by your question is not what your expecting


Try out array_merge instead:

$array = array('Username' => 'user', 'Email' => 'email'); $array = array_merge($array, array('Password' => 'pass'));

This produces the array:

array('Username' => 'user', 'Email' => 'email', 'Password' => 'pass');


Associative arrays aren't designed to have their keys in order. You can add an element via

$array['Password'] = 'pass';