爆炸和处理PHP动态数组 [英] explode and process php dynamic arrays

查看:164
本文介绍了爆炸和处理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'].'" >

$ pestname ['scoutlogpestname'] 总是可以是不同的。在 $的obj ['card_id的'] 可以是任何从值1-50 。我把在名称搞清楚,我需要一个分隔符与爆炸。
它打印数组看起来像这样。任何值输入数字和空白任何没有进入。

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
)

有人可以请给我如何执行爆炸循环,这样我可以处理一些有识之士的 $ pestname ['scoutlogpestname'] $ OBJ ['card_id的'] 值?感谢您寻找。

推荐答案

失去怪异的事情哈希值,只是使用数组表示法中的表单域。例如...

Lose that weird hash thing and just use array notation in your form fields. For example...

<input name="<?= htmlspecialchars($pestname['scoutlogpestname']) ?>[<?= $obj['card_id'] ?>]" ...

这会产生一种像

<input name="Aphids[1]" ...
<input name="Aphids[2]" ...

在提交时,这会给你的数组的数组的 $ _ POST 超级全球,如:

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

这篇关于爆炸和处理PHP动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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