简体   繁体   English

JSON数组到对象多维

[英]Json array to object multidimensional

The issue at hand is that I have an array within my JSON after I decoded it. 即将出现的问题是,在解码后,我的JSON中有一个数组。

I collect a few items from the database, and place them in an array in an foreach. 我从数据库中收集了一些项目,并将它们放在foreach中的数组中。

Data to place in array: 要放置在数组中的数据:

[ 0 =>
        [ 0 => [
            'title' => 'Title 1',
            'files' => [
                'name' => 'file_name',
                'url' => 'file_url'
            ]
        ]
        ],
        [ 1 => [
            'title' => 'Title 1',
            'files' => [
                'name' => 'file_name',
                'url' => 'file_url'
            ]
        ]
        ],
        [ 2 => [
            'title' => 'Title 3',
            'files' => [
                'name' => 'file_name',
                'url' => 'file_url'
            ]
        ]
        ],
        [ 3 => [
            'title' => 'Title 4',
            'files' => [
                'name' => 'file_name',
                'url' => 'file_url'
            ]
        ]
        ]
    ]

Next I'll place it in an array to place all files belonging to the same title : 接下来,我将其放置在数组中,以放置属于同一title所有文件:

$dataArray = [];
    foreach ($array as $key => $value) {
        $dataArray['dataInfo'][] = [
            'title' => $value['title'],
            'files' => [
                'name' => $value['files']['name'],
                'url' => $value['files']['url']
            ]
        ];
    }

Then I'll convert it to an JSON with: 然后,我将其转换为JSON:

json_encode(dataArray);

The result is: 结果是:

{
      "dataInfo": [
        {
            "title": "Title 1",
          "files": {
            "name": "file name",
            "url": "file_url"
          }
        },
        {
            "title": "Title 1",
          "files": {
            "name": "file name",
            "url": "file_url"
          }
        },
        {
            "title": "Title 3",
          "files": {
            "name": "file name",
            "url": "file_url"
          }
        },
        {
            "title": "Title 4",
          "files": {
            "name": "file name",
            "url": "file_url"
          }
        }
      ]
    }

What I want is: 我想要的是:

{
        "dataInfo": [
        {
            "title": "Title 1",
          "files": {
            "name": "file name",
            "url": "file_url",
          }, 
          {
            "name": "file name",
            "url": "file_url",
          }
        },
        {
            "title": "Title 3",
          "files": {
            "name": "file name",
            "url": "file_url"
          }
        },
        {
            "title": "Title 4",
          "files": {
            "name": "file name",
            "url": "file_url"
          }
        }
      ]
    }

How can I make this happen? 我怎样才能做到这一点?

This should work I guess: 我猜这应该工作:

$dataArray = [];
$previousTitle = '';
foreach ($array as $key => $value) {
    if ($previousTitle === $value['title']) {
        $dataArray['dataInfo'][$previousTitle]['files'] += [
            'name' => $value['files']['name'],
            'url' => $value['files']['url']
        ]
    } else {
        $dataArray['dataInfo'][] = [
            'title' => $value['title'],
            'files' => [
                'name' => $value['files']['name'],
                'url' => $value['files']['url']
            ]
        ];
    }

    $previousTitle = $value['title'];
}

Laravel has the collections that will allow you do do this: Laravel提供的集合可让您做到这一点:

$result = [ "dataInfo" => 
    collect($dataArray["dataInfo"])
        ->groupBy('title')
        ->map(function ($group, $title) {                    
        return [
            "title" => $title,
            "files" => $group->pluck("files")->all()
        ];
    })
];

Breaking this down: 分解如下:

You first transform $dataArray["dataInfo"] by making it a collection then grouping it by "title". 首先,将$dataArray["dataInfo"]转换$dataArray["dataInfo"]一个集合,然后按“ title”对其进行分组。 Then each group is transformed into an entry with title and an array of the files. 然后将每个组转换为带有标题和文件数组的条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM