0

我正在获取 CSV 数据并使用 PHP 生成 JSON,但我想知道是否可以从 CSV 中获取某些行以在对象中创建数组,请参阅下面的目标以清楚地了解:

我有以下示例 CSV 数据:

songId, title, artists:name[1], artists:main[1], artists:name[2], artists:main[2]
"trk-01", "Track 1", "artist 1", "true", "artist 2", "false"
"trk-02", "Track 2", "artist 3", "true", "artist 4", "false"

(这可以格式化以适应解决方案)

以下 PHP 用于从上述 CSV 创建 JSON:

if (($handle = fopen('example.csv', 'r')) === false) {
    die('Error opening file');
}
$headers = fgetcsv($handle, 1024, ',');
$complete = array();
while ($row = fgetcsv($handle, 1024, ',')) {
    $complete[] = array_combine($headers, $row);
}
$completeobj = array("tracks" => $complete);
$result = array_merge($completeobj);
fclose($handle);

echo json_encode($result, JSON_PRETTY_PRINT);
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($result, JSON_PRETTY_PRINT));
fclose($fp);

这会生成如下所示的 JSON:

{
    "tracks": [
        {
            "songId": "trk-01",
            "title": "Track 1",
            "artists:name[1]": "artist 1",
            "artists:main[1]": "true",
            "artists:name[2]": "artist 2",
            "artists:main[2]": "false"
        },
        {
            "songId": "trk-02",
            "title": "Track 2",
            "artists:name[1]": "artist 3",
            "artists:main[1]": "true",
            "artists:name[2]": "artist 4",
            "artists:main[2]": "false"
        }
    ]
}

但我的目标是在 CSV 中获取这些“艺术家”行并将它们的值放入对象艺术家中的数组中,请参见下面的所需 json:

{
    "tracks": [
        {
            "songId": "trk-01",
            "title": "track 1",
            "artists":
            [
                {
                   "name": "Artist 1",
                   "main": true              
                },
                {
                   "name": "Artist 2",
                   "main": false              
                }
            ]
        },
        {
            "songId": "trk-02",
            "title": "track 2",
            "artists":
            [
                {
                   "name": "Artist 3",
                   "main": true              
                },
                {
                   "name": "Artist 4",
                   "main": false              
                }
            ]
        }
    ]
}

如果有人可以提出实现此目标的方法或向我展示一个可以帮助我弄清楚的示例,那将是王牌!

4

1 回答 1

2

在你的 json_encode 之前试试这个

$new_result = array();
foreach($result['tracks'] as $trackdata){
    $id = $trackdata['songId'];
    $title = $trackdata['title'];
    $name1 = $trackdata['artists:name[1]'];
    $name2 = $trackdata['artists:name[2]'];
    $main1 = $trackdata['artists:main[1]'];
    $main2 = $trackdata['artists:main[2]'];

    $new_result['tracks'][]=array(
        'songID'    => $id,
        'title'     => $title,
        'artists'   => array( 
            0 => array(
                'name' => $name1,
                'main' => $main1,
                ),
            1 => array(
                'name' => $name2,
                'main' => $main2,
            ),
        ),
    );
}
于 2013-05-24T18:00:50.920 回答