Reputation: 181
With the help of variable categories i have this array in which all the arrays are without key/default index , because of this when i do $categories as $category in foreach and when i echo $category['name'] it gives illegal sting offet.
using laravel blade
What can be the possible solution for this or should i validate array first?
Upvotes: 2
Views: 449
Reputation: 1050
I think this is your situation, and in my opinion, you are using the wrong variable to decode let me take your image as an example here.
<?php
$ex = [
'result' => 'success',
'categories' => [
[
'id'=>1,
'name'=>'cat1',
],
[
'id'=>2,
'name'=>'cat21',
],
[
'id'=>3,
'name'=>'cat31',
],
]
];
echo "<pre>";
$res = json_encode($ex);
print_r(json_decode($res));
in this example, I just showed you that I have done json_encode and json_decode easily without any issue and the output of JSON is same as your example image.
Thank You!
Upvotes: 1
Reputation: 146
The above code is in JSON format, convert to array first
for example
$ex = {
'result':"success",
'categories':[{
//your rest of the code
}]
}
$data = json_decode($ex, TRUE);
//next use for each
foreach($data as $key => $value)
{
//rest of your logic
}
Upvotes: 3