I'm building json sub-array structure (from CSV) using the following PHP:
$tracks = array();
foreach ($result['tracks'] as $trackdata) {
$songId = $trackdata['songId'];
$title = $trackdata['title'];
$ISRC = $trackdata['ISRC'];
$name1 = $trackdata['artists:name[1]'];
$name2 = $trackdata['artists:name[2]'];
$main1 = $trackdata['artists:main[1]'];
$main2 = $trackdata['artists:main[2]'];
$tracks['tracks'][] = array(
'songId' => $songId,
'title' => $title,
'ISRC' => $ISRC,
'artists' => array(
0 => array(
'name' => $name1,
'main' => (boolean) $main1
),
1 => array(
'name' => $name2,
'main' => (boolean) $main2
)
)
);
}
Currently, if the CSV contains empty values the json (correctly) displays the value as NULL.
What would I need to add to my PHP to stop a particular object from being part of the json if its value is empty / blank?
I'm specifically looking to not print artists:name[2] and artists:main[2] if a second artists doesn't exist in the CSV data.
Here is an example of the CSV code:
"trk-04","track 4","USAM18490006","John Smith",true,,
I've researched (and attempted to implement) if (empty... but I'm not sure where that would come in this code.
Any pointers would be great.
Thanks in advance!