麻烦来自多个领域的数据转换成一个阵列 [英] Trouble getting data from multiple fields into one array

查看:122
本文介绍了麻烦来自多个领域的数据转换成一个阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的形式:

I have a form that looks like this:

<form name="search" method="post" action="http://example.com/search3/">
Seach for: <input type="text" name="find[]" /> 
Search for stories by Type
<select name="find[]">
<option value="Fiction" selected>Fiction</option>
<option value="Non-Fiction">Non-Fiction</option>
<option value="Essay">Essay</option>
</select>
<input type="submit" name="search" value="Search" />
</form>

在php.search,我通过获取数据:

on php.search, I get the data by:

$input = array(
"find" => $_POST['find'],
);

这几乎是工作,但它并没有把结果查找数组中为止。相反,数据会成数组2的结果,可能是B / C我命名为每场找到[]。这里是后续代码var_dump:

It is almost working, except it doesn't put the results in the find array. Instead the data is going into the results in array2, probably b/c i named each field find[]. Here is the var_dump:

array(1) {
["find"]=>
array(2) {
[0]=>
string(5) "testing1"
[1]=>
string(7) "testing2"
}
}

我不知道为什么我离开查找阵列空白在这种情况下。

I'm not sure why i leaves the find array blank in this case.

如果我改变形式,以便这一发现[]的名称作为数组变得只是觉得,
然后我得到进入指定的数组中的数据发现像我想要的,但是,只有选择表格数据将被捕获。见的var_dump的结果,我怎么只得到一个结果,而不是两个:

If I change the form so the find[] names as arrays become just find, then I get the data to go into the array named find like i want, HOWEVER, only the select form data will be captured. See var_dump for results and how I only get one result instead of two:

array(1) {

["find"]=>
string(7) "testing2"

}

所以,问题是...
我怎样才能查找数组中捕获的输入,并选择数据?

SO THE QUESTION IS... How can I get the input AND select data captured in the find array?

推荐答案

在用方括号 [] 命名场,PHP会自动在<$ C嵌套数组$ C> $ _ POST (或 $ _ GET )阵列。

When naming field using square brackets [], PHP automatically creates a nested array in the $_POST (or $_GET) array.

在你的情况,$输入['发现']的值有两个数字键的数组。

In your case, the value of $input['find'] is an array that has two numeric keys.

echo $input['find'][0];
echo $input['find'][1];

您可以假设指数0持有文本输入输入的值,该指数1持有该列表中选择的值。

You can assume that index 0 holds the value entered in text input, the index 1 holds the value selected in the list.

如果你跳过字段名称squre支架,浏览器将它们发送到服务器,如下所示:

If you skip the squre brackets in field names, browsers sends them to the server as follows:

find=value1&find=value2

PHP,关键在分析数据时,首先将值1 找到,但当另一个找到键时,它只是会覆盖新的值2的previous值

PHP, when parsing data, first stores the value1 under the key find, but when another find key occurs, it just overwrites the previous value with new value2.

这篇关于麻烦来自多个领域的数据转换成一个阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆