【问题标题】:How do I create a loop based off this array?如何创建基于此数组的循环?
【发布时间】:2010-03-29 20:59:23
【问题描述】:

我正在尝试处理这个数组,首先测试是否存在支票,然后根据数量推断数据以返回有效价格。

这是固定数量项目的输入,没有可变数量。

<input type="checkbox" name="measure[<?=$item->id?>][checked]" value="<?=$item->id?>">
<input type="hidden" name="measure[<?=$item->id?>][quantity]" value="1" />

这是可变数量项目的输入。

<input type="checkbox" name="measure[<?=$item->id?>][checked]" value="<?=$item->id?>"> 
<input class="item_mult" value="0" type="text" name="measure[<?=$item->id?>][quantity]" />

因此,结果数组是多维的。这是一个输出:

Array ( 
[1] => Array ( [quantity] => 1 ) 
[2] => Array ( [quantity] => 1 ) 
[3] => Array ( [quantity] => 1 ) 
...
[14] => Array ( [checked] => 14 [quantity] => 999 ) 
) 

这是我用来获取此数组并首先处理从表单中选中的项目的循环。我想这个问题本质上归结为我如何构造我的条件语句来合并多维数组?

foreach($field as $value):  
            if ($value['checked'] == TRUE) { 

                $query = $this->db->get_where('items', array('id' => $value['checked']))->row();

                #Test to see if quantity input is present
                if ($value['quantity'] == TRUE) {
                    $newprice = $value['quantity'] * $query->price;

                    $totals[] = $newprice;  
                }

                #Just return the base value if not
                else { 
                    $newprice = $query->price;

                    $totals[] = $newprice; 
                }

            }
            else { } ?>

            <p><?=$query->name?> - <?=money_format('%(#10n', $newprice)?></p>

        <? endforeach; ?>

我的问题:如何更改循环以使用多维数组?

【问题讨论】:

  • 它是这样工作的吗?还是有某种错误的输出? $field 是整个数组还是其中的一行?
  • foreach 循环没有运行,它中断了。如果我说得对的话,我正在寻找“foreach through”的正确键和值。
  • 您收到什么错误/警告?循环本身对我来说看起来不错(除了检查不存在索引$value[checked] 的通知,但这不会破坏循环)。
  • 未定义索引:选中

标签: php input foreach


【解决方案1】:

我发现了至少 2 个错误:

  1. money_format 调用中将 $newprice 的声明放在 main if 的范围之外,否则在它之外不可用
  2. 在检查数组的值之前,请检查该值是否使用isset 函数设置。

所以代码是

foreach($field as $value):
            $newprice = 0;  
            if (isset($value['checked']) && $value['checked'] == TRUE) { 

                $query = $this->db->get_where('items', array('id' => $value['checked']))->row();

                #Test to see if quantity input is present
                if (isset($value['quantity']) &&  $value['quantity'] == TRUE) {
                    $newprice = $value['quantity'] * $query->price;

                    $totals[] = $newprice;  
                }

                #Just return the base value if not
                else { 
                    $newprice = $query->price;

                    $totals[] = $newprice; 
                }

            }
            else { } ?>

            <p><?=$query->name?> - <?=money_format('%(#10n', $newprice)?></p>

        <? endforeach; ?>

【讨论】:

    【解决方案2】:

    您不应该在 if 语句中与 true 进行比较。只需使用裸表达式,让 PHP 检查其“真实性”:

            if ($value['checked']) { 
                ....
                if ($value['quantity']) {
    

    【讨论】:

    • 你当然是对的,但这并没有打破循环,因为它没有类型比较。
    猜你喜欢
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    相关资源
    最近更新 更多