首页 > 解决方案 > 试图从嵌套数组中提取一组值

问题描述

我一直在尝试从嵌套数组中提取一组值以导出到 csv 中。

我尝试使用指针来设置 csv 的标题,但没有得到它。

$header = $data(array("items" => array("snippet" => array("topLevelComment" => array("snippet" => array("authorDisplayName", "authorChannelUrl", "textOriginal", "publishedAt", "updatedAt", "viewerRating"))))));


$filename = 'commentsData.csv';
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$filename");
$output = fopen("php://output", "w");
$header = array_keys([snippet]);
fputcsv($output, $header);
foreach($data as $row)
{
    fputcsv($output, $row);
}

fclose($output);

预期结果应该是

authorDisplayName: brian
authorChannelUrl: www.test.com
textOriginal: this is awesome
publishedAt: 12:00:00 15/12/2019
updatedAt:
viewerRating:

这就是数组的样子

stdClass::__set_state(array(
   'kind' => 'youtube#commentThreadListResponse',
   'etag' => '"nlUZBA6NbTS7q9G8D1GljyfTIWI/TXVS0MgTUYyoqidO2WPNLXKrJ30"',
   'nextPageToken' => 'QURTSl9pMnZENGVEWGpGdkxfbWF4SVZBa0JoSTJMdXctcUYxTWJSQmVBWGtKQmdYWlVEbkVmR2ExOGgyS0htQTJaa2tFVU93YWc3ODNZbUIxZ0p2QVFzZmNZNTg2b0RGdmRuV1l1dmVrby05OXFXT0ttcm43MWNXazZSVUprcXVaM3hlTXJtdzMxZjdVNGhiM0VxSDRmS1Z2aThSZjBv',
   'pageInfo' => 
  stdClass::__set_state(array(
     'totalResults' => 100,
     'resultsPerPage' => 100,
  )),
   'items' => 
  array (
    0 => 
    stdClass::__set_state(array(
       'kind' => 'youtube#commentThread',
       'etag' => '"nlUZBA6NbTS7q9G8D1GljyfTIWI/iODOvhn8hmfZdb0gzAYSXmgkuV8"',
       'id' => 'Ugwt095BDucaSH2wkfl4AaABAg',
       'snippet' => 
      stdClass::__set_state(array(
         'videoId' => '6p1e_JSol8s',
         'topLevelComment' => 
        stdClass::__set_state(array(
           'kind' => 'youtube#comment',
           'etag' => '"nlUZBA6NbTS7q9G8D1GljyfTIWI/lo4HGIF_UPUMzIxybqOkxd6CNxo"',
           'id' => 'Ugwt095BDucaSH2wkfl4AaABAg',
           'snippet' => 
          stdClass::__set_state(array(
             'authorDisplayName' => 'Phillipe',
             'authorProfileImageUrl' => 'https://yt3.ggpht.com/-4VPqeHeObVk/AAAAAAAAAAI/AAAAAAAAAAA/j-aEePrvrh8/s28-c-k-no-mo-rj-c0xffffff/photo.jpg',
             'authorChannelUrl' => 'http://www.youtube.com/channel/UCB47Hn7PvF_eeqtegVrxg2Q',
             'authorChannelId' => 
            stdClass::__set_state(array(
               'value' => 'UCB47Hn7PvF_eeqtegVrxg2Q',
            )),
             'videoId' => '6p1e_JSol8s',
             'textDisplay' => 'Oculus Rift S, anyone? Linus forgot to mention it entirely!!!!!!! No need for extra cameras at all, for $ 400, it’s a good VR headset...',
             'textOriginal' => 'Oculus Rift S, anyone? Linus forgot to mention it entirely!!!!!!! No need for extra cameras at all, for $ 400, it’s a good VR headset...',
             'canRate' => true,
             'viewerRating' => 'none',
             'likeCount' => 791,
             'publishedAt' => '2019-08-13T00:00:28.000Z',
             'updatedAt' => '2019-08-13T00:00:28.000Z',
          )),
        )),
         'canReply' => true,
         'totalReplyCount' => 88,
         'isPublic' => true,
      )),
    )),

标签: php

解决方案


$items = array();
foreach($data['items'] as $data2){
    $data3 = $data2['snippet']['topLevelComment']['snippet'];
    $data4 = array('authorDisplayName' => $data3['authorDisplayName'],'authorChannelUrl' => $data3['authorChannelUrl'],'textOriginal' => $data3['textOriginal'],'publishedAt' => $data3['publishedAt'],'updatedAt' => $data3['updatedAt'],'viewerRating' => $data3['viewerRating']);
    array_push($items,$data4);
}

```

Was able to use this foreach loop to get the data out.

推荐阅读