在更新查询中得到空白值

时间:2015-11-24 07:51:03

标签: php arrays loops

这里我通过传递数组来更新for循环中的查询。 由于array_shift,id可以在3 and 4上运行,但是当我通过3 and 7时,第二个ID不可见

我的阵列:

$altxt = array( "", "test", "","", "", "test1", "", "", "", "");
$chk = array( "3", "7" );

for($i=0; $i < sizeof($altxt); $i++)
{
    if(!empty($altxt[$i]))
    {
        echo "<br>";
        echo "update order_details set process_order='1' tracking_number=$altxt[$i] where id=".  array_shift( $chk );
    }
}

输出给予:

update order_details set process_order='1' tracking_number=test where id=3
update order_details set process_order='1' tracking_number=test1 where id=

正如我想要的输出:

update order_details set process_order='1' tracking_number=test where id=3
update order_details set process_order='1' tracking_number=test1 where id=7

2 个答案:

答案 0 :(得分:2)

您可以使用array_filterforeach循环来简化此操作:

$altxt = array_values(array_filter(array(
  "", "test", "","", "", "test1", "", "", "", ""
)));
$chk = array("3", "7");

foreach ($altxt as $key => $value) {
   echo "<br />update order_details set process_order='1' tracking_number='$value' where id=" . $chk[$key];
}

更新:使用array_values重新索引已过滤的数组,以匹配两个数组的键。

答案 1 :(得分:0)

试试这个:

for($i=0; $i < sizeof($altxt); $i++)
{
    if(!empty($altxt[$i]))
    {
        echo "<br>";
        echo "update order_details set process_order='1' tracking_number=$altxt[$i] where id=".  array_shift( $chk );
        unset($chk[$i]);
    }
}