如果array为空,如何从json_encode中排除它? (PHP)

时间:2013-05-27 21:17:04

标签: php json

我正在使用以下PHP构建json子数组结构(来自CSV):

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

目前,如果CSV包含空值,则json(正确)将值显示为NULL。

如果其值为空/空白,我需要添加到PHP以阻止特定对象成为json的一部分吗?

我特别希望不打印艺术家:名称[2] 艺术家:main [2] 如果CSV数据中不存在第二位艺术家

以下是CSV代码的示例:

"trk-04","track 4","USAM18490006","John Smith",true,,

我已经研究过(并尝试实现)if(空...但我不确定这段代码会出现在哪里。

任何指针都会很棒。

提前致谢!

5 个答案:

答案 0 :(得分:2)

您需要避免将第二个艺术家添加到随后进行json编码的中间数组中。

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

如果您愿意,可以使用empty功能代替is_null。它将考虑诸如空字符串和数字0之类的名称不存在。 is_null只能捕获严格=== null的那些。

答案 1 :(得分:1)

像这样:

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

答案 2 :(得分: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
);

答案 3 :(得分:0)

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

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

}

更好的

$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++;

}

答案 4 :(得分:0)

使用array_filter从数组中过滤掉未设置的日期

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

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