提问者:小点点

json数组到对象多维


目前的问题是,在解码JSON后,JSON中有一个数组。

我从数据库中收集了一些项目,并将它们放在foreach中的一个数组中。

要放置在数组中的数据:


    [ 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'
            ]
        ]
        ]
    ]

接下来,我将把它放在一个数组中,以放置属于同一标题的所有文件


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

然后,我将使用以下命令将其转换为JSON:

json_encode(dataArray);

结果是:


    {
      "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"
          }
        }
      ]
    }

我想要的是:


    {
        "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"
          }
        }
      ]
    }

我怎样才能做到这一点?


共2个答案

匿名用户

我想这应该行得通:

$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的系列将允许您这样做:

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

分解如下:

首先转换$dataArray[“dataInfo”],方法是将其设置为集合,然后按“title”对其进行分组。然后将每个组转换为一个带有标题的条目和一个文件数组。