PHP -在数组中插入元素


PHP - Insert elemens into an array

我正在编写一个公式,但目前我只想将我的元素(我有书籍和作者)插入到数组中。

我可以用作者(姓名+姓氏)和foreach显示我的书,但是我不能添加更多的元素。

下面是代码的形式:

<H1>Exercice 2</H1>
<form method="POST">
    <label for"code" >Number :</label>
    <input id="code" name="code" type="number" />
    <label for"title">Title :</label>
    <input id="title" name="title" type="text" />
    <label for"author" >Author :</label>
    <input id="author" name="author" type="text" />
<button type="input" type="submit">Ok</button>

$title =  $_POST['title']; 
$code = $_POST['code'];
$author = $_POST['author'];
$book = array();
$book['code'] = 123;
$book['title'] = "Legendes";
$book['author'] = array("David", "Gemmel");
foreach($book as $value){
    $book['key'] = $value;
    var_dump($book);
    if (is_array($value)) {
        foreach($value as $otherValue) {
            echo($otherValue);
        }
    } else {
        echo($value);
    }
}

我做了一些搜索,但我不认为它的工作,它使用的array_push()方法与POST,但我不知道在哪里我可以操纵我的形式到数组。

如果你想知道一些细节,我很乐意做=)我正在努力,如果我有什么消息,你会知道的=)

祝你今天愉快=)

1)赋值是相反的。正确的方法:

$myVar = $myValue
2)你需要在你的输入中设置name属性,以便发送:
<input id="code" type="number" name="code" />

那么你可以这样访问它们:

$_POST['code']
$array['key'] = $value;

你的练习2有一些错误:

首先,您的HTML输入必须具有通过post检索的name属性:
<h1>Exercice 2</h1>
<form method="post">
    <label>
       <input name="code" type="number" />
    </label>
    <button type="submit">Ok</button>
</form>

在PHP中,您可以使用名称访问任何输入值:

$code = $_POST['code'];

现在,我认为您想要使用这个HTML表单"添加"几本书,而不需要存储系统。问题是你不能这样做,如果每一个新的请求,因为你在你的数组中的所有元素将被删除,每次你运行一个新的post请求。为了保存这些信息,您需要使用一些持久存储系统作为数据库或其他。

由于您似乎希望将每本书的信息保存在一起,因此需要使用多维数组—因此,您需要重做整个操作。我有个建议:

形式:

<h2>Exercice 2</h2>
<form method="post">
<label for"code">Number :</label>
<input id="code" name="code" type="number">
<label for"title">Title :</label>
<input id="title" name="title" type="text">
<label for"author-firstname">Author First Name:</label>
<input id="author-firstname" name="author-firstname" type="text">
<label for "author-lastname">Author Last Name:</label>
<input id="author-lastname" name="author-lastname" type="text">
<input type="submit" name="submit_book" value="Ok">
</form>

修复了名称问题,改变了标题(你从来没有,从来没有使用H1的形式,H1是严格用于网站范围的标题/标志/网站名称)。还将按钮更改为简单的input type="submit" .

$title = $_POST['title'];
$code = $_POST['code'];
$author = $_POST['author'];
$book = []; // changed this to modern PHP version array assignment
$book[0]['code'] = 123;
$book[0]['title'] = "Legendes";
$book[0]['author-firstname'] = "David";
$book[0]['author-lastname'] = "Gemmel"; // no reason to assign a separate array for first and last name, just use two array-keys
for ($c = 0; $c <= count($book); $c++) { //changed this to a for, counting the amount of entries in the $book array
    echo 'Title: '.$book[$c]['title'];
    echo 'Author: '.$book[$c]['author-firstname'].' '.$book[$c]['author-lastname'];
} // the content should probably be wrapped in a container of some sort, probably a <li> (and then a <ul>-list declared before the for-loop)

。这些都与把东西放入数组没有任何关系。就像这样(甚至没有必要为您发布的代码分配$ _post变量)。但是,你可以这样做:

if (isset($_POST['submit_book'])) {
    $title = $_POST['title'];
    $code = $_POST['code'];
    $author-firstname = $_POST['author-firstname'];
    $author-lastname = $_POST['author-lastname'];
    // however, if all you're doing is putting this into the array, no need to assigne the $_POST to variables, you can just do this:
    $temp_array = ['code'=>$_POST['code'],'title'=>$_POST['title'],'author-firstname'=>$_POST['author-firstname'],'author-lastname'=>$_POST['author-lastname']];
    $book[] = $temp_array;
}

那么,这将取代代码开头的赋值变量