dscrb00804 2018-08-30 18:39
浏览 22
已采纳

PHP - 访问数组中的数据

Can't seem to work out how to access data within an array that has other arrays within it.

My array looks like:

Array
(
    [0] => Array
        (
            [_id] => 28
            [serv_image] => 
            [serv_name] => My Service
            [slug] => my-service
            [is_featured] => 
            [_blocks] => Array
                (
                    [0] => Array
                        (
                            [anchor_heading] => Mats Heading
                            [_block_type] => anchorHeading
                            [_block_id] => pe8t69
                            [_block_index] => 0
                        )

                    [1] => Array
                        (
                            [anchor_heading] => Another anchored heading
                            [_block_type] => anchorHeading
                            [_block_id] => pea49u
                            [_block_index] => 1
                        )

                )

            [_page] => *
            [_pageID] => 1
            [_sortvalue] => 1003
        )

)

So far I've tried this without success:

foreach($services as $service) {
    print_r($service['_blocks']['anchor_heading']);
}

This results in undefined index on anchor_heading.

UPDATE

I will have an unknown amount of these that I need to group together into a variable. How can I ensure I'm grabbing them all, no matter how many other times it's been entered?

  • 写回答

3条回答 默认 最新

  • donglu2761 2018-08-30 18:43
    关注

    You might use 2 times a foreach and echo the value of anchor_heading:

    foreach($services as $service) {
        foreach ($service["_blocks"] as $block) {
            echo $block["anchor_heading"];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题