Php 如何基于此数组创建循环?

Php 如何基于此数组创建循环?,php,input,foreach,Php,Input,Foreach,我试图处理这个数组,首先测试是否存在支票,然后根据数量推断数据以返回有效价格 这是固定数量项目的输入,没有可变数量 <input type="checkbox" name="measure[<?=$item->id?>][checked]" value="<?=$item->id?>"> <input type="hidden" name="measure[<?=$item->id?>][quantity]" value="

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

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

<input type="checkbox" name="measure[<?=$item->id?>][checked]" value="<?=$item->id?>">
<input type="hidden" name="measure[<?=$item->id?>][quantity]" value="1" />
这是我用来获取此数组并首先处理表单中已检查项的循环。我猜问题本质上归结为如何构造条件语句以合并多维数组

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; ?>
foreach($fieldas$value):
如果($value['checked']==TRUE){
$query=$this->db->get_where('items',array('id'=>$value['checked']))->row();
#测试以查看是否存在数量输入
如果($value['quantity']==TRUE){
$newprice=$value['quantity']*$query->price;
$totals[]=$newprice;
}
#如果不是,只返回基值
否则{
$newprice=$query->price;
$totals[]=$newprice;
}
}
还有{}?>
-


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

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

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

我发现至少有两个错误:

  • $newprice
    声明置于main if范围之外,否则在
    money\u格式的调用中它不可用
  • 在检查数组的值之前,请检查是否使用
    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; ?>
    
    foreach($fieldas$value):
    $newprice=0;
    如果(isset($value['checked'])&&$value['checked']==TRUE){
    $query=$this->db->get_where('items',array('id'=>$value['checked']))->row();
    #测试以查看是否存在数量输入
    if(设置($value['quantity'])&&$value['quantity']==TRUE){
    $newprice=$value['quantity']*$query->price;
    $totals[]=$newprice;
    }
    #如果不是,只返回基值
    否则{
    $newprice=$query->price;
    $totals[]=$newprice;
    }
    }
    还有{}?>
    -


    它是这样工作的吗?还是有某种错误的输出?
    $field
    是整个数组还是其中的一行?foreach循环不运行,它会中断。如果我说得对的话,我正在寻找“foreach through”的正确键和值。您会得到什么错误/警告?循环本身在我看来很好(除了检查不存在的索引
    $value[checked]
    ,但这不会破坏循环)。未定义索引:checked您当然是对的,但这不会破坏循环,因为它不是类型比较。
            if ($value['checked']) { 
                ....
                if ($value['quantity']) {
    
    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; ?>