如何将多行json数组转换为php对象数组以访问该数组的属性

时间:2019-03-25 18:10:52

标签: php json laravel

我有一个从AJAX响应(刀片到控制器)发送的Json多行数组,我再次将该数组传递给局部视图。

如何在laravel刀片视图中访问此数组的所有属性?

我尝试过但徒劳无功:

(object)$array "-AND-" json_decode($array, true)

这是我在laravel刀片视图上得到的JSON数组:

[{ "id": 1, "name": "Water", "type": "amenity", "active": 1, "created": "2019-03-15 01:09:01", "pivot": { "property_type_id": 3, "amenity_id": 1 } }, 
 { "id": 2, "name": "Electricity", "type": "amenity", "active": 1,
"created": "2019-03-15 00:44:02", "pivot": { "property_type_id": 3, "amenity_id": 2 } }, 
 { "id": 3, "name": "Sui-Gas", "type": "amenity", "active": 1, "created": "2019-03-15 00:44:02", "pivot": { "property_type_id": 3,
"amenity_id": 3 } }, 
 { "id": 4, "name": "Telephone", "type": "amenity", "active": 1, "created": "2019-03-15 00:58:59", "pivot": { "property_type_id": 3, "amenity_id": 4 } }, 
 { "id": 5, "name": "Lawn \/ Garden", "type":
"moreAmenity", "active": 1, "created": "2019-03-15 01:17:58", "pivot": { "property_type_id": 3, "amenity_id": 5 } }, 
 { "id": 6, "name": "Store Room", "type": "moreAmenity", "active": 1, "created": "2019-03-15 01:17:35",
"pivot": { "property_type_id": 3, "amenity_id": 6 } }, 
 {"id": 7, "name": "Laundry Area", "type": "moreAmenity", "active": 1, "created": "2019-03-15 01:17:35", "pivot": { "property_type_id": 3, "amenity_id": 7 } }, 
 {"id": 8, "name": "Garage \/ Parking", "type": "moreAmenity", "active": 1, "created": "2019-03-15 01:18:45", "pivot": { "property_type_id": 3, "amenity_id": 8 } }]

我想通过转换或解码来访问此数组的每个属性。

2 个答案:

答案 0 :(得分:0)

您可以像这样将JSON数据发送到视图:

view('view-name')->with('data', json_decode($array, true));

请参见Laravel Views

并在您的视图中像这样使用它:

@foreach($data as $value)
    ID: {{ $value['id'] }}
    Name: {{ $value['name'] }}
    // .. and so on ..
@endforeach

请参见Laravel Templates

答案 1 :(得分:0)

首先,“多行json”是什么意思?如果字符串是有效的json(无论内容是什么),则可以使用内置的 json_decode 函数将json字符串转换为数组或对象,然后将数组/对象传递给查看:

$jsonString = '[{"id":1,"name":"Water","type":"amenity","active":1,"created":"2019-03-15 01:09:01","pivot":{"property_type_id":3,"amenity_id":1}},{"id":2,"name":"Electricity","type":"amenity","active":1,"created":"2019-03-15 00:44:02","pivot":{"property_type_id":3,"amenity_id":2}},{"id":3,"name":"Sui-Gas","type":"amenity","active":1,"created":"2019-03-15 00:44:02","pivot":{"property_type_id":3,"amenity_id":3}},{"id":4,"name":"Telephone","type":"amenity","active":1,"created":"2019-03-15 00:58:59","pivot":{"property_type_id":3,"amenity_id":4}},{"id":5,"name":"Lawn \/ Garden","type":"moreAmenity","active":1,"created":"2019-03-15 01:17:58","pivot":{"property_type_id":3,"amenity_id":5}},{"id":6,"name":"Store Room","type":"moreAmenity","active":1,"created":"2019-03-15 01:17:35","pivot":{"property_type_id":3,"amenity_id":6}},{"id":7,"name":"Laundry Area","type":"moreAmenity","active":1,"created":"2019-03-15 01:17:35","pivot":{"property_type_id":3,"amenity_id":7}},{"id":8,"name":"Garage \/ Parking","type":"moreAmenity","active":1,"created":"2019-03-15 01:18:45","pivot":{"property_type_id":3,"amenity_id":8}}]';
$objects = json_decode($json);
//Or alternatively: $array = json_decode($json, true);

然后,您可以将结果传递到视图(当然还有其子视图):

return vew('some-view')->with(['objects'=>$objects]);

并在视图中使用for或foreach指令:

@foreach($objects as $object)
$object->id <br/> $object->name <br/> $object->pivot->property_type_id
@endforeach