php Foreach 用于数组内的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10131802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:27:46  来源:igfitidea点击:

Foreach for arrays inside of an array

phparraysforeach

提问by user979331

I have an array and i did a print_r of it and it returned this...

我有一个数组,我对它做了一个 print_r ,它返回了这个......

Array ( [0] => Array ( [invoiceid] => 2 [client] => Test 1 [invoicedeliverymethod] => email [paymenttimeallotment] => 15 [clientid] => 1 [date] => 2012-04-12 [enddate] => 2012-04-02 00:00:00 [total] => 250.86 [remainingbalance] => 250.86 [ispaid] => No [isagentpaid] => No [datedistributed] => Not distributed [invoicedcontact] => 1 ) [1] => Array ( [invoiceid] => 1 [client] => Test 1 [invoicedeliverymethod] => email [paymenttimeallotment] => 15 [clientid] => 1 [date] => 2012-04-12 [enddate] => 2012-03-31 23:59:59 [total] => 602.29 [remainingbalance] => 602.29 [ispaid] => No [isagentpaid] => No [datedistributed] => 2012-04-12 [invoicedcontact] => 1 ) )

I tried to run this code...

我试图运行这段代码...

 foreach($resultArray as $row => $value){
    echo $value . "<br/>";
}

and that returned Array Array.

并返回数组数组。

How do I get the values inside of these arrays?

如何获取这些数组中的值?

回答by Alex Siri

You need to iterate the elements from the inside array, like this:

您需要迭代内部数组中的元素,如下所示:

foreach($resultArray as $row => $innerArray){
  foreach($innerArray as $innerRow => $value){
    echo $value . "<br/>";
  }
}

回答by Jürgen Paul

foreach($resultArray as $row => $value){
    foreach($value as $row2 => $value2)
        echo $value2 . "<br/>";
}