Search code examples
phparraysjsonmodx

Datastream Array Returning Only First Value


I am using the MODX cms to output data from a third party, and I am stumped on how to return a nested array, currently I am only getting it to return the first array or NULL. Here is a sample of the code I am using:

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

I am able to take the values that return ok and place them in my code, but the nested array is only returning the first value, and I'm not sure if I am doing this correctly:

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

Thank you very much in advance for any help


Solution

  • Hope this helps You :

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