How to loop through array of multiple arrays in php

前端 未结 2 408
滥情空心
滥情空心 2021-01-28 01:11

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)

相关标签:
2条回答
  • 2021-01-28 01:22

    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
    }
    
    0 讨论(0)
  • 2021-01-28 01:23

    Use a loop to build 2 separate arrays:

    foreach($array['ExpensesList'] as $index => $val){
        $array1[$index] = $array['ExpensesList'][$index][0];
        $array2[$index] = $array['ExpensesList'][$index][1];
    }
    

    Then insert each array into your database individually.

    This will not work if any sub array contains an index at 2, so this is explicitly for the example structure you provided.

    0 讨论(0)
提交回复
热议问题