2016-07-16 6 views
1

MODX CMSを使用して第三者からデータを出力しています。ネストされた配列を返す方法がわかりません。現在、最初の配列またはNULLを返すようになっています。ここで私が使用していたコードのサンプルです:最初の値だけを返すDatastream配列

$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

ありがとうございます!これは問題を理解するための正しい道を私に与えました、私はちょうどもっと多くの行を追加しなければなりませんでしたが、これは私が必要としていたものです。 – Robert

+0

喜んで助けてください!ハッピーコーディング – pradeep

関連する問題