Json encode hakkında yardıma ihtiyacım var |
34 Mesajlar | 3.759 Okunma |
$json = array();
$json[]["data"] = $val;
echo json_encode($json,JSON_FORCE_OBJECT);
{,…}
0:{data: {0: {id: "1", category: "real_estate", title: "aaaaaaa", location: "63 Birch Street",…},…}}
data:{0: {id: "1", category: "real_estate", title: "aaaaaaa", location: "63 Birch Street",…},…}
0:{id: "1", category: "real_estate", title: "aaaaaaa", location: "63 Birch Street",…}
1:{id: "2", category: "real_estate", title: "bbbbbbbb", location: "63 Birch Street",…}
data: [{id: 1, category: "real_estate", title: "Steak House Restaurant", location: "63 Birch Street",…},…]
$someArray = [];
foreach($val as $s){
array_push($someArray, [
'id' => $s['id'],
'title' => $s['title'],
'latitude' => $s['latitude'],
'longitude' => $s['longitude'],
'location' => $s['location']
]);
}
echo json_encode($someArray,JSON_FORCE_OBJECT);
{,…}
0:{id: "1", title: "aaaaaaa", latitude: "51.541599", longitude: "-0.112588", location: "63 Birch Street"}
1:{id: "2", title: "aaaaaaa", latitude: "51.541599", longitude: "-0.112588", location: "63 Birch Street"}
{,…}
data:[{id: 1, category: "real_estate", title: "Steak House Restaurant", location: "63 Birch Street",…},…]
0:{id: 1, category: "real_estate", title: "Steak House Restaurant", location: "63 Birch Street",…}
1:{id: 2, category: "bar_restaurant", title: "Blue Chilli", location: "1882 Trainer Avenue",…}
{
"data": [
{
"id": 1,
"category": "real_estate",
"title": "Steak House Restaurant",
"location": "63 Birch Street",
"latitude": 51.541599,
"longitude": -0.112588,
"url": "item-detail.html",
"type": "Apartment",
"type_icon": "assets/icons/store/apparel/umbrella-2.png",
"rating": 4,
"gallery":
[
"assets/img/items/1.jpg",
"assets/img/items/5.jpg",
"assets/img/items/4.jpg"
],
"features":
[
"Free Parking",
"Cards Accepted",
"Wi-Fi",
"Air Condition",
"Reservations",
"Teambuildings",
"Places to seat"
],
"date_created": "2014-11-03",
"price": "$2500",
"featured": 0,
"color": "",
"person_id": 1,
"year": 1980,
"special_offer": 0,
"item_specific":
{
"bedrooms": 2,
"bathrooms": 2,
"rooms": 4,
"garages": 1,
"area": 240
},
"description": "Curabitur odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet pellentesque mauris. Proin sit amet scelerisque risus. Donec semper semper erat ut mollis. Curabitur suscipit, justo eu dignissim lacinia, ante sapien pharetra duin consectetur eros augue sed ex. Donec a odio rutrum, hendrerit sapien vitae, euismod arcu.",
"last_review": "Curabitur odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet",
"last_review_rating": 5
}
]
}
{,…}
data:[{id: 1, category: "real_estate", title: "Steak House Restaurant", location: "63 Birch Street",…},…]
0:{id: 1, category: "real_estate", title: "Steak House Restaurant", location: "63 Birch Street",…}
1:{id: 2, category: "bar_restaurant", title: "Blue Chilli", location: "1882 Trainer Avenue",…}
2:{id: 3, title: "Eddie’s Fast Food", category: "real_estate", location: "1882 Trainer Avenue",…}
3:{id: 4, title: "Saguaro Tavern", location: "1882 Trainer Avenue", latitude: 51.539212,…}
4:{id: 5, title: "Max Five Lounge", location: "1882 Trainer Avenue", latitude: 51.52234,…}
5:{id: 6, title: "Lavadome", location: "1882 Trainer Avenue", latitude: 51.503965, longitude: -0.058837,…}
6:{id: 7, title: "Café Roosterville Kitchen", location: "1882 Trainer Avenue", latitude: 51.486758,…}
7:{id: 8, title: "Magma Bar and Grill", location: "1882 Trainer Avenue", latitude: 51.555385,…}
8:{id: 9, title: "Guilt Trattoria", location: "1882 Trainer Avenue", latitude: 51.560935,…}
9:{id: 10, title: "Hot Dogma", location: "1882 Trainer Avenue", latitude: 51.530189, longitude: -0.07875,…}
10:{id: 11, title: "Roadhouse Cantina", location: "1882 Trainer Avenue", latitude: 51.543803,…}
11:{id: 12, title: "Jetstream", location: "1882 Trainer Avenue", latitude: 51.518334,…}
12:{id: 13, title: "Benephisto Tavern", location: "1882 Trainer Avenue", latitude: 51.527306,…}
//Veritabanı sorguları vs.
// $all değişkenini veritabanınızdan aldığınız array olarak varsayıyorum
header('Content-type: application/json');
echo '{
"data":[';
$item_count = count($all);
$numb = '0';
foreach ($all as $one ) {
echo '{
"id":'.$one["id"].',
"category":"'.$one["category"].'",
"title":"'.$one["title"].'",
"location":"'.$one["location"].'",
"latitude":'.$one["latitude"].',
"longitude":'.$one["longitude"].',
"url":"item-detail.html",
"type":"Apartment",
"type_icon":"assets/icons/store/apparel/umbrella-2.png",
"rating":4,
"gallery":[
"assets/img/items/1.jpg",
"assets/img/items/5.jpg",
"assets/img/items/4.jpg"
],
"features":[
"Free Parking",
"Cards Accepted",
"Wi-Fi",
"Air Condition",
"Reservations",
"Teambuildings",
"Places to seat"
],
"date_created":"2014-11-03",
"price":"$2500",
"featured":0,
"color":"",
"person_id":1,
"year":1980,
"special_offer":0,
"item_specific":{
"bedrooms":2,
"bathrooms":2,
"rooms":4,
"garages":1,
"area":240
},
"description":"Curabitur odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet pellentesque mauris. Proin sit amet scelerisque risus. Donec semper semper erat ut mollis. Curabitur suscipit, justo eu dignissim lacinia, ante sapien pharetra duin consectetur eros augue sed ex. Donec a odio rutrum, hendrerit sapien vitae, euismod arcu.",
"last_review":"Curabitur odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet",
"last_review_rating":5
}';
$numb++;
if ( $numb != $item_count ) { echo ','; }
}
echo ']
}';