0

I am taking CSV data and using PHP to generate JSON, but I'd like to know if it's possible to take certain rows from the CSV to create arrays within an object, see goal below for clarity:

I have the following example CSV data:

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"

(This can be formatted to suit the solution)

The following PHP is being used to create JSON from the above CSV:

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);

This is generating JSON that looks like:

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

But my goal is to take those 'artists' rows in the CSV and put their values into an array within the object artists, see desired json below:

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

If anyone could suggest a method of achieving this or show me an example that may help me to figure it out that would be ace!

3
  • Don’t use array_combine, but just create the desired sub-array structure yourself in the while loop …? Commented May 24, 2013 at 11:06
  • Good shout @CBroe - I'll give that a try. Commented May 24, 2013 at 11:23
  • @CBroe if you happen to know of a good example of this, would you mind chucking me a link? Commented May 24, 2013 at 11:26

1 Answer 1

2

try this before your 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,
            ),
        ),
    );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.