PHP - 将elemens插入数组中

时间:2016-10-24 19:40:41

标签: php arrays post

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

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

以下是表单的代码。

$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);
    }
}

public static int Prims(Vector<Vector<node>> adjList)

我做了一些searcch,但我觉得它不起作用,它使用了带有POST的array_push()方法,但我不知道在哪里可以将我的表单操作到数组中。

如果你想要一些细节,我会很乐意这样做=)我正在努力,如果我有一些新闻,你会知道=)

度过愉快的一天=)

3 个答案:

答案 0 :(得分:0)

1)作业相反。正确的方式:

$myVar = $myValue

2)您需要在输入中设置名称属性才能发送:

<input id="code" type="number" name="code" />

然后您可以访问它们,如:

$_POST['code']

3)要在数组中按键添加元素,请使用:

$array['key'] = $value;

答案 1 :(得分:0)

你的练习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请求时都会删除数组中的所有元素。要保留此信息,您需要将某些持久存储系统用作数据库或其他。

答案 2 :(得分:0)

由于您似乎想要将每本书的信息保存在一起,因此您需要使用多维数组 - 因此,您需要重做整个事物。这是一个建议:

形式:

<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;
}

因此,这将替换代码开头的指定变量。