2

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!

5 Answers 5

2

You will want to avoid adding the second artist to the intermediate array that you subsequently json-encode.

$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]'];

    $artists = array();
    // First artist always exists
    $artists[] = array('name' => $name1, 'main' => (boolean) $main1);
    // Add second artist only if one exists with a non-null name
    if(!is_null($name2)) {
      $artists[] = array('name' => $name2, 'main' => (boolean) $main2);
    }

    $tracks['tracks'][] = array(
        'songId' => $songId,
        'title' => $title,
        'ISRC' => $ISRC,
        'artists' => $artists
    );
}

You can use the empty function instead of is_null if you wish. It will consider names such as empty strings and the number 0 as not existing. is_null will only catch those which are strictly === null.

Sign up to request clarification or add additional context in comments.

1 Comment

This using !empty got me the result I was looking for. Many thanks! :)
1

like this :

if (!empty($songId))
{
    $tracks['tracks']['songId'] = $songId;
}
if (!empty($title))
{
    $tracks['tracks']['title'] = $title;
}
and so on ...

Comments

1
$artists[0] = array(
            'name' => $name1,
            'main' => (boolean) $main1
        );
if (!empty($name2)&&!empty($main2))
    $artists[1] = array(
            'name' => $name2,
            'main' => (boolean) $main2
        );
$tracks['tracks'][] = array(
    'songId' => $songId,
    'title' => $title,
    'ISRC' => $ISRC,
    'artists' => $artists
);

Comments

0

use array_filter to filter out unset date from an array

 // assuming $artist is an array you want to scrub

 $artist = array_filter($artist);
 $track["artists"][] = $artist;

Comments

0
if (  ( ! isset( artists:name[2] ))
   || ( empty( artists:name[2] ))
   )  {

  unset( $tracks['tracks'][ 'artists' ][ 1 ] );

}

Better

$n = 0;
while( ! empty( $trackdata[ 'artists:name[' . $n+1 . ']' ] )) {

   $tracks['tracks'][ 'artists' ][ $n ][ 'name' ] 
       = $trackdata[ 'artists:name[' . $n+1 . ']' ];

   $tracks['tracks'][ 'artists' ][ $n ][ 'main' ] 
       = $trackdata[ 'artists:main[' . $n+1 . ']' ];

  $n++;

}

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.