如何在PHP中循环遍历多个数组的数组 [英] How to loop through array of multiple arrays in php

查看:416
本文介绍了如何在PHP中循环遍历多个数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历php中的数组数组.通常有时候会遇到复杂的数组,但是我需要您的帮助.

I am trying to loop through array of arrays in php. Usually get stalked with complex array sometimes but I need your kind assistance with this.

var_dump($array)产生了以下数组:

    $arrayVal = array(6) {
      ["item_id"]=>
      array(2) {
        [0]=>
        string(1) "1"
        [1]=>
        string(1) "2"
      }
      ["request_explanation"]=>
      array(2) {
        [0]=>
        string(7) "Welcome"
        [1]=>
        string(11) "Hello World"
      }
      ["quantity"]=>
      array(2) {
        [0]=>
        string(1) "4"
        [1]=>
        string(1) "4"
      }
      ["unit_cost"]=>
      array(2) {
        [0]=>
        string(1) "4"
        [1]=>
        string(1) "3"
      }
      ["total_cost"]=>
      array(2) {
        [0]=>
        string(1) "0"
        [1]=>
        string(1) "0"
      }
      ["supporting_document"]=>
      string(0) ""
    }

我的数据库表:

我希望能够将该数组中的每个值保存到上表中.感谢您的帮助.

I want to be able to save each of the value in that array into the table above. Thanks for helping me.

推荐答案

使用子数组之一的索引访问所有其他子数组:

Use the indexes of one of the sub-arrays to access all the other sub-arrays:

foreach ($array['item_id'] as $i => $item_id) {
    $request_explanation = $array['request_explanation'][$i];
    $quantity = $array['quantity'][$i];
    // repeat this for all the columns
    // Now you can insert all these variables into the database
}

这篇关于如何在PHP中循环遍历多个数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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