douxian9706 2012-11-19 07:00
浏览 47
已采纳

在foreach中传递3个数组

In java code i have this

ArrayList<NameValuePair> al = new ArrayList<NameValuePair>();

for (int i = 0; i < alitems.size(); i++) {

    al.add(new BasicNameValuePair("items[]", alitems.get(i)));
    al.add(new BasicNameValuePair("qty[]", alnoof.get(i)));
    al.add(new BasicNameValuePair("price[]", alprice.get(i)));
}

Note: here alitems, alnoof, alprice are ArrayLists

php code

foreach (array_combine($_POST['items'], $_POST['qty']) as $val => $no) {

   $v = mysql_real_escape_string($val);
   $va = mysql_real_escape_string($no);
}

now i am able to get 2 array values of $v and $va but I am not able to get the price arraylist. How to do it?

suppose $_POST['items']={pencil,pen,watch}
        $_POST['qty']={2,3,4}
        $_POST['price']={20,30,40}

i need to insert values in database as :

pencil,2,20   pen,3,30   watch,4,40

Kindly help me on how to achieve this.

  • 写回答

1条回答 默认 最新

  • duannaoye0732 2012-11-19 07:03
    关注

    PHP also has for loop

    $_POST['items'] = ["pencil","pen","watch"];
    $_POST['qty'] = [2,3,4];
    $_POST['price'] = [20,30,40];
    
    for($i = 0; $i < count($_POST['items']); $i ++) {
        echo $_POST['items'][$i], " ", $_POST['qty'][$i], " ", $_POST['price'][$i], PHP_EOL;
    }
    

    Or

    foreach ( array_map(null, $_POST['items'], $_POST['qty'], $_POST['price']) as $var ) {
        list($item, $qty, $price) = $var;
        echo $item, " ", $qty, " ", $price, PHP_EOL;
    }
    

    Please not that [] is only supported in PHP >= 5.4 other whise you have to use array()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程