json_from_db:
{
"id": 23,
"positions": "[{\"col\":2,\"row\":1,\"size_x\":10,\"size_y\":3}]",
"styles": "\"position: absolute; min-width: 135px; min-height: 160px;\"",
"created_at": "2016-07-18 02:40:36",
"updated_at": "2016-07-18 02:40:36"
}
Looping in PHP:
$positions = json_from_db
foreach($positions->position as $value) { echo $value->col}
Invalid argument supplied error
I want to get the value of col, sixe_x and size_y
Update
Answers below are correct, but want to get values of styles as well
Try my source xD
$positions = json_decode('{
"id": 6,
"positions": [
{
"col": 6,
"row": 1,
"size_x": 2,
"size_y": 2
}
],
"created_at": "2016-07-17 23:36:47",
"updated_at": "2016-07-17 23:36:47"
}');
echo 'col: '.$positions->positions[0]->col.'<br>';
echo 'row: '.$positions->positions[0]->row.'<br>';
echo 'size_x: '.$positions->positions[0]->size_x.'<br>';
echo 'size_y: '.$positions->positions[0]->size_y.'<br>';
You will get result
col: 6
row: 1
size_x: 2
size_y: 2
You need to use json_decode()
.
$arr = '{"id":6,"positions":" [{\"col\":6,\"row\":1,\"size_x\":2,\"size_y\":2}]","created_at":"2016-07-17 23:36:47","updated_at":"2016-07-17 23:36:47"}';
$positionsArr = json_decode($arr,true);
$positions = json_decode($positionsArr["positions"],true);
foreach($positions as $value)
{
echo "size x:". $value['size_x']. "\n size y: ". $value['size_y'];
}
Live demo : Click Here
You need to decode the JSON before you can work with the data
$json_obj = json_decode($json_from_db);
$positions = $json_obj->positions;
foreach($positions as $position){
echo $position->col . "<br/>";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.