2

我正在使用以下 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 的一部分?

如果 CSV 数据中不存在第二个艺术家,我特别希望不打印艺术家:名称 [2]艺术家:主要 [2] 。

以下是 CSV 代码的示例:

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

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

任何指针都会很棒。

提前致谢!

4

5 回答 5

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 的。

于 2013-05-27T21:26:24.600 回答
1

像这样 :

if (!empty($songId))
{
    $tracks['tracks']['songId'] = $songId;
}
if (!empty($title))
{
    $tracks['tracks']['title'] = $title;
}
and so on ...
于 2013-05-27T21:23:01.070 回答
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
);
于 2013-05-27T21:26:55.853 回答
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++;

}
于 2013-05-27T21:23:15.383 回答
0

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

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

 $artist = array_filter($artist);
 $track["artists"][] = $artist;
于 2013-05-27T21:25:03.630 回答