|
|
|
php ทำไฟล์ json โดยการวน loop ตามตัวอย่างแบบนี้ ต้องเขียนอย่างไรครับ |
|
|
|
|
|
|
|
Code (PHP)
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"letter": "G",
"color": "blue",
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[123.61, -22.14], [122.38, -21.73], [123.61, -22.14]
]
]
}
}
]}
ถ้าต้องใช้ php ต้องเขียนอย่างไรครับ
Code (PHP)
$obj = new stdClass();
$obj->type="FeatureCollection";
$obj->features = array(
array('1999','3.0'),
array('2000','3.9'),
//and so on...
);
echo (json_encode($obj));
แบบนี้ยังไม่ใกล้เคียงเลยครับ
Tag : PHP, Ajax
|
ประวัติการแก้ไข 2016-10-21 21:42:52
|
|
|
|
|
Date :
2016-10-21 21:41:44 |
By :
iiop |
View :
1915 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"color": "blue",
บรรทัดข้างบนนี้ Syntax error นะครับ
Code (PHP)
<?php
$s = <<<'EEE'
{
"type": "FeatureCollection",
"features":
[
{
"type": "Feature",
"properties": {
"letter": "G",
"color": "blue"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[123.61, -22.14], [122.38, -21.73], [123.61, -22.14]
]
]
}
}
]
}
EEE;
echo '<pre>';
echo var_export(json_decode($s,true),1);
echo json_last_error_msg();
|
|
|
|
|
Date :
2016-10-21 23:56:35 |
By :
num |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ประมาณนี้ ลองเอาไปรันดูครับ
Code (PHP)
$x = array('123.61','122.38','123.61');
$y = array('-22.14','-21.73','-22.14');
$data=array();
$numRow = count($x);
for($i=0;$i<$numRow;$i++){
$resules=array(
$x[$i],
$y[$i]
);
array_push($data, $resules);
}
echo json_encode($data);
|
|
|
|
|
Date :
2016-10-24 10:58:22 |
By :
thesin18598 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แทน {} ด้วย object --> (object)array()
แทน [] ด้วย array();
แทน : ด้วย =>
Code
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"letter": "G",
"color": "blue",
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[123.61, -22.14], [122.38, -21.73], [123.61, -22.14]
]
]
}
}
]}
Code (PHP)
$a = (object)array(
"type"=>"FeatureCollection",
"features"=>array(
(object)array(
"type"=>"Feature",
"properties"=>(object)array(
"letter"=>"G",
"color"=>"blue",
),
"geometry"=>(object)array(
"type"=>"Polygon",
"coordinates"=>array(
array(
array(123.61, -22.14), array(122.38, -21.73), array(123.61, -22.14)
)
)
)
)
)
);
|
|
|
|
|
Date :
2016-10-24 12:01:17 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|