Sorry for the duplicate question, but I am not able to render json_decod multi-dimensional array in template file.
I also tried using ArrayList and ArrayData but it is working with a single array not with multi-dimensional array.
Any other method is there for the same?
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
ArrayList/ArrayData works fine, but you'll have to specify iterations as a list.
public function testLoop() {
return ArrayList::create([
1 => ['foo'=>['bar'=>'hello']],
2 => ['foo'=>['bar'=>'world']],
]);
}
<% loop $testLoop %>
$foo.bar
<% end_loop %>
public function testWith() {
return ArrayData::create([
'foo'=>['bar'=>'hello']
]);
}
<% with $testWith %>
$foo.bar
<% end_with %>
Thanks @Devlin for this working code, but I am little confused with my json data.
$jsonData = json_decode($response->getBody(), true);
$output = ArrayList::create([
$jsonData
]);
When I print this I got result like
ArrayList Object
(
[items:protected] => Array
(
[0] => Array
(
[Page] => 1
[PageCount] => 1
[MinPrice] => 46
[MaxPrice] => 187
[RecordCount] => 3
[SearchResults] => Array
(
[0] => Array
(
[Hotel] => Array
(
[Address] => ZZZZZZZZ
[City] => ZZZZZZZZ
[Country] =>
[County] => ZZZZZZZZ
[Fax] =>
[Id] => 52
[Name] => ZZZZZZZZ
[Phone] =>
[Postcode] => ZZZZZZZZ
[Website] =>
=>
[Photos] => Array
(
[0] => Array
(
[Id] => 0
[Source] => ZZZZZZZZ
[Thumbnail] => ZZZZZZZZ
[Medium] => ZZZZZZZZ
[Default] =>
[Order] => 0
)
)
[DescriptionHotelAmenities] =>
[DescriptionRoomAmenities] =>
)
[Price] => 46
[OldPrice] => 46
)
[1] => Array
(
[Hotel] => Array
(
[Address] => ZZZZZZZZ
[City] => ZZZZZZZZ
[Country] =>
[County] => ZZZZZZZZ
[Fax] =>
[Id] => 47
[Name] => ZZZZZZZZ
[Phone] =>
[Postcode] => ZZZZZZZZ
[Website] =>
[Email] =>
[Photos] => Array
(
[0] => Array
(
[Id] => 0
[Source] => ZZZZZZZZ
[Thumbnail] => ZZZZZZZZ
[Medium] => ZZZZZZZZ
[Default] =>
[Order] => 0
)
)
[DescriptionHotelAmenities] =>
[DescriptionRoomAmenities] =>
)
[Price] => 177.65
[OldPrice] => 177.65
)
)
)
)
[failover:protected] =>
[customisedObject:protected] =>
[objCache:ViewableData:private] => Array
(
)
[class] => ArrayList
[extension_instances:protected] => Array
(
)
[beforeExtendCallbacks:protected] => Array
(
)
[afterExtendCallbacks:protected] => Array
(
)
)[/code]
Your json_decode array looks something like this:
$jsonData = [
'MinPrice' => 46,
'MaxPrice' => 187,
'SearchResults' => [
0 => [
'Hotel' => [
'Address' => 'ZZZZZZZZ',
'Id' => 52,
'Photos' => [
0 => [
'Id' => 0,
'Source' => 'ZZZZZZZZ',
]
],
],
'Price' => 46,
],
1 => [
'Hotel' => [
'Address' => 'ZZZZZZZZ',
'Id' => 47,
'Photos' => [
0 => [
'Id' => 0,
'Source' => 'ZZZZZZZZ',
]
],
],
'Price' => 177.65,
]
]
];
To use it in a template it should look something like this:
public function testList() {
return ArrayList::create([
0 => [
'MinPrice' => 46,
'MaxPrice' => 187,
'SearchResults' => ArrayList::create([
0 => [
'Hotel' => [
'Address' => 'ZZZZZZZZ',
'Id' => 52,
'Photos' => ArrayList::create([
0 => [
'Id' => 0,
'Source' => 'ZZZZZZZZ',
]
]),
],
'Price' => 46,
],
1 => [
'Hotel' => [
'Address' => 'ZZZZZZZZ',
'Id' => 47,
'Photos' => ArrayList::create([
0 => [
'Id' => 0,
'Source' => 'ZZZZZZZZ',
]
]),
],
'Price' => 177.65,
]
])
]
]);
}
<% loop $testList %>
$MinPrice<br>
$MaxPrice<br>
<% loop $SearchResults %>
$Hotel.Address<br>
$Hotel.Id<br>
<% loop $Hotel.Photos %>
$Id<br>
$Source<br>
<% end_loop %>
$Price<br>
<% end_loop %>
<% end_loop %>
So you have to parse $jsonData like:
public function testList() {
$jsonData = []; // see above
$jsonData = Convert::raw2xml($jsonData); // because all input data is evil
$myResults = ArrayList::create();
foreach($jsonData['SearchResults'] as $searchResult) {
$myResult = $searchResult;
$myResult['Hotel']['Photos'] = ArrayList::create();
foreach($searchResult['Hotel']['Photos'] as $photo) {
$myResult['Hotel']['Photos']->push($photo);
}
$myResults->push($myResult);
}
return ArrayList::create([
0 => [
'MinPrice' => $jsonData['MinPrice'],
'MaxPrice' => $jsonData['MaxPrice'],
'SearchResults' => $myResults,
]
]);
}
@Devlin Thanks a lot, you are my hero :) and thanks for detail explanation