如何在使用表单的逗号开头的现有数组末尾添加新数组?

I'm trying to submit a form along with the "First" & "Last" name, when the form is submitted, How do I insert those new names into the existing array starting with a comma (so the PHP file doesn't break with white blank space).

I've tried this several times, but no use at all.

This is the PHP file called: "Arrays.php"

<?php
    $array_demo = array
    (
        // list of peoples names
        'John' => 'Wright'
    );
?>

This is the HTML form called: index.php

<form action="" method="POST">
<input type="text" name="firstname"><br>
<input type="text" name="lastname"><br>
<button type="submit">Add Names</button>
</form>

Any suggestions fellow StackOverflow Members? Trying to add those submitted-form fields inside an existing array as shown above.

The best way to do this is JSON serializing. It is more human readable and you'll get better performance. I do not know why you are trying to save the array itself in the php file.

$array_demo = array('John' => 'Wright');

//if you want to add a new name, Then you can do
$array_demo[$_POST['firstname']]= $_POST['lastname'];

//Then store the array to a file
file_put_contents("array.json",json_encode($array_demo));
# array.json => {"John":"Wright"}

//Then you can load the file back to an array
$array_demo = json_decode(file_get_contents('array.json'), true);

to match the current structure you can do this

$array_demo[{$_POST['firstname']}]= $_POST['lastname'];

but remember keys are unique so you could not have 2 people with the same first name

EXPANDING to basic php:

add:

action="Arrays.php"

to the form

then in Arrays.php:

$array_demo=array();//if the array is not already initialized.

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['firstname']) && isset($_POST['lastname'])){
    $array_demo[{$_POST['firstname']}]= $_POST['lastname'];

}