Php 仅返回第一个值的数据流数组

Php 仅返回第一个值的数据流数组,php,arrays,json,modx,Php,Arrays,Json,Modx,我正在使用MODX cms从第三方输出数据,我被困在如何返回嵌套数组的问题上,目前我只能让它返回第一个数组或NULL。下面是我正在使用的代码示例: $datastream = '[{"id":57, "offer_info":{"offer_headline":"Main Offer Headline", "published_title":"Displayed Main Title"}, // My issue is here with the nested array "service_c

我正在使用MODX cms从第三方输出数据,我被困在如何返回嵌套数组的问题上,目前我只能让它返回第一个数组或NULL。下面是我正在使用的代码示例:

$datastream = '[{"id":57,
"offer_info":{"offer_headline":"Main Offer Headline",
"published_title":"Displayed Main Title"},

// My issue is here with the nested array
"service_categories":[{"service_category_code":"D",
"headline":"My Headline for D",
"midline":"Middle copy",
"footline":"Footer copy for D"},

{"service_category_code":"T",
"headline":"My Headline for T",
"image":"image.svg",
"footline":"Footer copy for T"}]}]';

$output_json = json_decode($datastream);
$output_json = json_decode($datastream, true); // this returns null for everything

foreach($output_json as $component) {
$productid = $component->id; // this returns ok
$offer_headline = $component->offer_info->offer_headline; // this returns ok
$published_title = $component->offer_info->published_title; // this returns ok

$service_categories_category_code = $component->service_categories[0]->service_category_code; // this only returns first value

$service_categories_category_code = $component->service_categories->service_category_code; // this returns NULL

var_dump($service_categories_category_code);
我可以获取返回ok的值并将其放入代码中,但嵌套数组只返回第一个值,我不确定是否正确:

if ($service_categories_category_code = "D") {
    $d_details_headline = $component->service_categories[0]->headline;
    $d_details_midline = $component->service_categories[0]->midline;
    $d_footline = $component->service_categories[0]->footline;
} elseif ($service_categories_category_code = "T") {
    $t_details_headline = $component->service_categories[0]->headline;
    $t_details_image = $component->service_categories[0]->image;
    $t_details_footline = $component->service_categories[0]->footline;
};

提前非常感谢您的帮助

希望这对您有所帮助:

        <?php 

        $datastream = '[{"id":57,"offer_info":{"offer_headline":"Main Offer
        Headline","published_title":"Displayed Main 
        Title"},"service_categories":[{"service_category_code":"D",
        "headline":"My Headline for D","midline":"Middle 
        copy","footline":"Footer copy for D"},{"service_category_code":"T",
        "headline":"My Headline for T",
        "image":"image.svg",
        "footline":"Footer copy for T"}]}]';

         $output_json = json_decode($datastream);
      //$output_json = json_decode($datastream, true); // this returns null for everything
        $data = array();
        foreach($output_json as $component) {
        $productid = $component->id; // this returns ok
        $offer_headline = $component->offer_info->offer_headline; // this returns ok
        $published_title = $component->offer_info->published_title; // this returns ok
        $data['productid'] = $component->id; 
        $data['offer_info']['offer_headline'] = $component->offer_info->offer_headline; 
        $data['offer_info']['published_title'] = $component->offer_info->published_title; 
        foreach ($component->service_categories as $comp) {
          if ($comp->service_category_code == 'D') {
           $data['D']['service_category_code'] =  $comp->service_category_code;
           $data['D']['headline'] =  $comp->headline;
           $data['D']['midline'] =  $comp->midline; 
           $data['D']['footline'] =  $comp->footline;
         } elseif ($comp->service_category_code == 'T') {
           $data['T']['service_category_code'] =  $comp->service_category_code;
          $data['T']['headline'] =  $comp->headline; 
          $data['T']['image'] =  $comp->headline;
          $data['T']['footline'] =  $comp->footline;
      }
   }

  print_r($data);
 }

非常感谢!这让我找到了解决问题的正确途径,我只需要再添加几行,但这正是我所需要的,让它帮助你!快乐编码