dousuo8400 2018-11-08 21:00
浏览 47

在PHP中循环使用多维数组

So I have an Array set up as follows

Array
(
    [0] => App\Model\Entity\Member Object
        (
            [id] => 20
            [fname] => John
            // ...
            [member_attribute] => Cake\ORM\Entity Object
                (
                    [id] => 13
                    [membership_status] => Active
                    [last_payment] => 1541581496
                    // ...
                 )
)

I'm using a foreach loop for the first level

foreach ($aArray as $Member => $Value)

However I can't access the [member_attributes] data because it's in its own array.

Any help?

  • 写回答

1条回答 默认 最新

  • dpwtr666638 2018-11-08 21:08
    关注

    The items in your array are objects, so use object notation to access them:

    foreach ($aArray as $Member => $Value) {
        $id = $Value->member_attribute->id;
    }
    

    There also is no need to use the key => value form of the foreach, you could just access your Members like:

    foreach ($aArray as $Member) {
        $id = $Member->member_attribute->id;
    }
    

    That may be a matter of preference, however, I think it is more clear.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类