爆炸并处理php动态数组
I have a form that is built dynamically. Here's the form input
echo'<input class="input stickyinput" type="number" name="'.$pestname['scoutlogpestname'].'#'.$obj['card_id'].'" >
The $pestname['scoutlogpestname']
can always be different. The $obj['card_id']
can be any value from 1-50
. I placed the #
in the name figuring I would need a delimiter to explode with.
The array that prints looks like this. Numbers for any values entered and blank for any not entered.
Array
(
[Aphids#1] => 11
[Thrips#1] => 5
[White-Fly#1] => 7
[Aphids#2] =>
[Thrips#2] => 1
[White-Fly#2] => 22
[Aphids#3] => 4
[Thrips#3] => 1
[White-Fly#3] =>
etc....... possibly to 50
)
Can somebody please give me some insight on how to execute the explode loop so I can process the $pestname['scoutlogpestname']
and the $obj['card_id']
values? Thanks for looking.
我有一个动态构建的表单。 这是表单输入 p>
echo'&lt; input class =“input stickyinput”type =“number”name =“'。$ pestname ['scoutlogpestname']。'#'。$ obj ['card_id']。'”&gt; \ n code> pre>
$ pestname ['scoutlogpestname'] code>总是不同的。 $ obj ['card_id'] code>可以是 1-50 code>中的任何值。 我将# code>放在名称中,我需要一个分隔符来爆炸。
打印的数组看起来像这样。 输入的任何值的数字和未输入的任何值的空白。 p>
数组
(
[蚜虫#1] =&gt; 11
[蓟马#1] =&gt ; 5
[White-Fly#1] =&gt; 7
[Aphids#2] =&gt;
[Thrips#2] =&gt; 1
[White-Fly#2] =&gt; 22 \ n [蚜虫#3] =&gt; 4
[蓟马#3] =&gt; 1
[白飞#3] =&gt;
等.......可能到50
) n code> pre>
有人可以给我一些关于如何执行爆炸循环的见解,这样我就可以处理 $ pestname ['scoutlogpestname'] code>和 $ obj ['card_id'] code>值? 谢谢你的期待。 p>
div>
Lose that weird hash thing and just use array notation in your form fields. For example...
<input name="<?= htmlspecialchars($pestname['scoutlogpestname']) ?>[<?= $obj['card_id'] ?>]" ...
This will produce something like
<input name="Aphids[1]" ...
<input name="Aphids[2]" ...
When submitted, this will give you an array of arrays in the $_POST
super global, eg
Array
(
[Aphids] => Array
(
[1] => 11
[2] =>
)
)
You can then iterate each entry and value array, eg
foreach ($_POST as $pestname => $values) {
foreach ($values as $card_id => $value) {
// code goes here
}
}
If you need to iterate all fields, then consider using a simple foreach and split the array keys based on "#":
// e.g. ['Aphids#1' => 11]
foreach ($array as $key => $value) {
list($pest_name, $card_id) = explode('#', $key, 2);
}
The variables $pest_name
and $card_id
are assigned a value based on the results of the explode()
operation.
See also: list
That said, storing values as part of a field name in that way is kind of clunky and it would be better to use an array syntax like Phil suggested in his answer.
Another way:
$array = array("Aphids#1" => 11,
"Thrips#1" => 5,
"White-Fly#1" => 7,
"Aphids#2" => 5,
"Thrips#2" => 1,
"White-Fly#2" => 22,
"Aphids#3" => 4,
"Thrips#3" => 1);
$data = array();
foreach ($array as $key => $value) {
$exploded = explode('#', $key);
$obj = array(
"pest_name" => $exploded[0],
"card_id" => $exploded[1],
"value" => $value
);
array_push($data, $obj);
}
echo "<pre>", print_r($data), "</pre>";
Will give you:
Array
(
[0] => Array
(
[pest_name] => Aphids
[card_id] => 1
[value] => 11
)
[1] => Array
(
[pest_name] => Thrips
[card_id] => 1
[value] => 5
)
[2] => Array
(
[pest_name] => White-Fly
[card_id] => 1
[value] => 7
)
[3] => Array
(
[pest_name] => Aphids
[card_id] => 2
[value] => 5
)
[4] => Array
(
[pest_name] => Thrips
[card_id] => 2
[value] => 1
)
[5] => Array
(
[pest_name] => White-Fly
[card_id] => 2
[value] => 22
)
[6] => Array
(
[pest_name] => Aphids
[card_id] => 3
[value] => 4
)
[7] => Array
(
[pest_name] => Thrips
[card_id] => 3
[value] => 1
)
)