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

时间:2016-07-16 21:52:45

标签: 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_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;
};

非常感谢您提前寻求帮助

1 个答案:

答案 0 :(得分:1)

希望这有助于您:

margin:0 auto;