打印嵌套数组关联数组php

Posted

技术标签:

【中文标题】打印嵌套数组关联数组php【英文标题】:Print nested array associative array php 【发布时间】:2019-05-28 21:16:03 【问题描述】:

我正在循环遍历一个数组,我想打印该数组的一些

我可以打印索引为 0 的第一个数组,但我不能打印子数组。

  @foreach($data[0] as $k => $v)
  $v['bill_no']  //prints b-0002
   @foreach($v['customer_details'] as $kk => $vv)
    Customer Name : $vv['name'] //gives the error
   @endforeach
 @endforeach

这是我的数组:

    Array ( [0] => 
    Array ( [0] => 
    Array ( [id] => 14 [customer_id] => 2 [referral_id] => 1 [sq_id] => 
    [bill_no] => b0002 [bill_date] => 07-09/2018 [payment_mode] => cash 
    [delivered_by] => tsr [net_total] => 15000
    [customer_details] => Array ( [id] => 2 [code] =>
    csdc [name] => jhon k [address] => mangalore [telephone] => cs [mobile] 
    => sd [tin] => sdc [gstin] => dsc
    [opening_balance] => 10000 [remarks] => sd [credit_limit] => 100 
    [bank_name] => HDFC [account_no] =>
    0022 [ifsc] => 5588 [account_name] => jhon [created_at] => 2018-08-13 
    22:04:10 [updated_at] => 2018-12-24
    09:39:00 ) [sold_items] => 

我要打印客户的名字

Name: $vv['name']

【问题讨论】:

试试$v['customer_details']['name'] 【参考方案1】:

试试这个:

@foreach($data[0] as $k => $v)
  $v['bill_no']  //prints b-0002
   @foreach($v['customer_details'] as $vv)    // As it is associative array, we are not going to use `$kk`
    Customer Name : $vv['name'] //Now this should work.
   @endforeach
 @endforeach

【讨论】:

以上是关于打印嵌套数组关联数组php的主要内容,如果未能解决你的问题,请参考以下文章

在 PHP 中创建关联数组的数组

与 PHP 中的数值数组相关联

与 PHP 中的数值数组相关联

嵌套数组和关联数组有啥区别?

php基本整理1

php判断数组是关联数组还是数值数组