dongshanni1611 2017-01-17 22: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 22: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 Windows Script Host 无法找到脚本文件"C:\ProgramData\Player800\Cotrl.vbs”
  • ¥15 matlab自定义损失函数
  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图