[英]how to retrieve a stdClass Object Array thats located inside another array
[英]Store Array thats Located Inside Object
我有以下JSON:
"list": {
"list_id": "2kA",
"title": "Social Media",
"description": "Trending",
"image": [
"http://cdn.socialmediaexaminer.com/wp-content/uploads/2012/11/br-campaign-report.png?9d7bd4",
"http://cdn.socialmediaexaminer.com/wp-content/uploads/2012/11/br-campaign-report.png?9d7bd"
],
"views": 65
}
如何在數據庫中存儲圖像數組的序列化版本? 執行以下任何操作都會返回錯誤:
$images = $item->list->image;
$images = $item->list->image->[0];
$images = $item->list->['image'];
多謝你們!
您可以像這樣訪問您的圖片:
foreach($item->list->image as $your_image) {
//do what you want
}
或使用這樣的關聯數組
$x = json_decode('{"list": {
"list_id": "2kA",
"title": "Social Media",
"description": "Trending",
"image": [
"a",
"b"
],
"views": 65
}}', true);
foreach($x['list']['image'] as $your_image) {
//do what you want
}
將它保存到您的數據庫使用json_encode(和轉義),作為mysqli連接的示例
$query = 'INSERT INTO your_table (...)
VALUES (\''.mysqli_real_escape_string($dblink, json_encode($your_array)).'\')';
在選擇你可以使用json_decode!
In your DB, data is in JSON, literally it means its a formatted string like:
"something here...."
You cannot access it by "->" as it is not an object.
So,
Convert your string(JSON) to Object to access like these:
$x['list']['image']
It can be achieved by json decoding your string which will convert your string to object
There was a bad error in code too. This one : $item->list->image->[0];
you cannot access an element of an array like this image->[0] --> it should be
$item->list->image[0]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.