0

次の 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'];

みんなありがとう!

4

2 に答える 2

1

you can access to your images like this:

foreach($item->list->image as $your_image) {
    //do what you want
}

or use a associated array like this

$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
}

to save it to your db use json_encode (and escape), as example of a mysqli connection

$query = 'INSERT INTO your_table (...)
          VALUES (\''.mysqli_real_escape_string($dblink, json_encode($your_array)).'\')';

on select you can use json_decode!

于 2013-02-19T06:36:26.797 に答える
1
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]
于 2013-02-19T06:45:28.903 に答える