dongshanni1611 2017-01-17 14:18
浏览 52
已采纳

处理数组以重新生成新数组

I make a call to an API and receive a JSON response which I then decode into an Array. The data I end up with is something like the following

Array
(
    [_id] => aasdasdasdasdasd
    [created] => 2017-01-16T14:11:54.616Z
    [options] => Array
        (
            [title] => 1
            [data] => Array
                (
                    [0] => Array
                        (
                            [labelName] =>  Date
                            [labelValues] => Array
                                (
                                    [0] => March 2016
                                )

                        )

                    [1] => Array
                        (
                            [labelName] => Title
                            [labelValues] => Array
                                (
                                    [0] => Food
                                )

                        )

                    [2] => Array
                        (
                            [labelName] => Product
                            [labelValues] => Array
                                (
                                    [0] => Rice
                                )

                        )

                )

        )
)

Because I am doing this in a loop, I receive many responses like the above. My aim is to extract the values from labelValues so I can create a folder structure.

Essentially, the above will generate an image, and for the above example the image should be placed in the following directory structure

2016 > Food > Rice > the generated image

I can handle creating the folder structure, but first I need to place the above data into an array. I would expect the array to look something like this

Array
(
    [0] => Array
        (
            [Date] => 2016
            [Title] => Food
            [Product] => Rice
        )

    [1] => Array
        (
            [Date] => second_loop_date
            [Title] => second_loop_title
            [Product] => second_loop_product
        )
    ...
)  

I have been trying to take it step by step to get the desired output, and I have been print things in order to debug along the way. I am sure there is a more effecient way compared to what I have done, but so far I have something like this

foreach ($output['options'] as $key => $value) {
    if($key == 'data') {
        foreach ($value as $label) {
            foreach ($label as  $labelValue) {
                foreach($labelValue as $output) {
                    print_r("<pre>");
                    print_r($output);
                    print_r("</pre>");
                }
            }
        }
    }
}

This kind of gets me to the values, but I also get a lot of warnings like

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\index.php on line 43

So really I was wondering what the best way to create this array would be. I have seen array_search, but not too sure if I can use it in this situation?

Any advice appreciated

展开全部

  • 写回答

3条回答 默认 最新

  • duansha6410 2017-01-17 14:23
    关注

    I think that you a little bit complicate your code. You can access directly $output['options']['data'] and it let you remove two outer loops.

    $results = array();
    foreach ($output['options']['data'] as $data) {
        if (isset($data['labelValues'][0])) {
            $results[$data['labelName']] = $data['labelValues'][0];
        }
    }
    

    Note: I simplified accessing labelValues, because it looks form your example array, that there is always only one item. In other case, you should use your inner loop to iterate over all labelValues and concatenate them into one string perhaps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部