Search code examples
phparraysdeclaration

"Warning: array_push(): First argument should be an array" while trying to push a value into a string-type value


$info = array(
        "First_Names" => "John",
        "Last_Names" => "Smith",
        "Gender" => "Male",
    );

array_push($info["First_Names"], "$fname");

print_r ($info);

I started learning PHP through a high school class. I'm not exactly knowledgeable nor do I pay attention much, but I'm completely stuck on this;

What I'm trying to do is get the variable $fname which is defined by the user (Jack, James, Shelly, etc) to be pushed into the array First_Names which is inside the array of $info. I'm not sure where it's going wrong, but PHP is not declaring $info as an array.

I think, it states:

Warning: array_push() [function.array-push]: First argument should be an array in /home/a4938424/public_html/process.php on line 22

If I print out the array it will show up the default names and gender,and if I echo out the $fname variable it shows up properly.)


Solution

  • $info['First_Names'] is not an array, it is a string ("John"). If you define it as an array, it might work ;)