Formatting json to geojson via PHP - php

I am trying to get some data that I have called from a mySQL database to correctly display in a GeoJSON format. Here's some of my PHP code:
$data = array(); //setting up an empty PHP array for the data to go into
if($result = mysqli_query($db,$query)) {
while ($row = mysqli_fetch_assoc($result))
{
$data[] = $row;
}
}
$jsonData =json_encode($data);
$original_data = json_decode($jsonData, true);
$coordinates = array();
foreach($original_data as $key => $value) {
$coordinates[] = array($value['latitude'], $value['longitude']);
}
$new_data = array(
'type' => 'FeatureCollection',
'features' => array(array(
'type' => 'Feature',
'properties' => array('time' => $value['time']),
'geometry' => array('type' => 'Point', 'coordinates' => $coordinates),
),
),
);
$final_data = json_encode($new_data, JSON_PRETTY_PRINT);
print_r($final_data);
I've managed to get my results to look like this so far:
But I need them to look like this so that every set of coordinates has its own "type" and "properties" key-value pair:
I've already found some help with this issue here, but I just can't manage to get over this last formatting hurdle...

Instead of building coordinates, you need to build features:
$data = array(); //setting up an empty PHP array for the data to go into
if($result = mysqli_query($db,$query)) {
while ($row = mysqli_fetch_assoc($result))
{
$data[] = $row;
}
}
$jsonData =json_encode($data);
$original_data = json_decode($jsonData, true);
$features = array();
foreach($original_data as $key => $value) {
$features[] = array(
'type' => 'Feature',
'properties' => array('time' => $value['time']),
'geometry' => array(
'type' => 'Point',
'coordinates' => array(
$value['latitude'],
$value['longitude'],
1
),
),
);
}
$new_data = array(
'type' => 'FeatureCollection',
'features' => $features,
);
$final_data = json_encode($new_data, JSON_PRETTY_PRINT);
print_r($final_data);

Related

How to add an array to a nested array

My PHP Code is the following, I need some assistance please:
//DB connection
$result = mysqli_query($con,"SELECT * FROM `clients`");
$info = array();
$count = 0;
$meta = array('page' => 1, 'pages' => 1, 'perpage' => -1, 'total' => 14, 'sort' => "asc", 'field' => "ID");
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$info[$row['clientid']]['id'] = $row['id'];
$info[$row['clientid']]['name'] = $row['name'];
$info[$row['clientid']]['email'] = $row['email'];
$info[$row['clientid']]['cell'] = $row['cell'];
$count++;
}
$data = json_encode(array_values($info));
echo $data;
My Result;
[{"ID":1,"name":"A","email":"a#a.com","cell":"082"},
{"ID":2,"name":"B","email":"b#b.com","cell":"083"},
{"ID":3,"name":"C","email":"c#c.com","cell":"079"}]
The JSON should add the meta array with the following result:
{"meta":
{"page": 1,"pages": 1,"perpage": -1,"total": 3,"sort": "asc","field": ID"},
"data": [{"ID":1,"name":"A","email":"a#a.com","cell":"082"},
{"ID":2,"name":"B","email":"b#b.com","cell":"083"},
{"ID":3,"name":"C","email":"c#c.com","cell":"079"}]
},
Create array of required structure and json_encode it:
$data = json_encode(array(
'meta' => $meta,
'data' => array_values($info),
));

How to bring values for each ID details in the array?

I was searching for a solution for my problem.
I have the code below:
while($row = mysql_fetch_array($resultQuery)){
$response = array(
'name' => $row['name'],
'data' => $row['data']
);
$responses[] = $response;
}
echo json_encode($responses);
The code will bring this result:
[{"name":"Name001","data":"1"},
{"name":"Name001","data":"2"},
{"name":"Name001","data":"3"},
{"name":"Name002","data":"4"},
{"name":"Name002","data":"5"},
{"name":"Name002","data":"6"}]
But I would like to have the result below:
[{"name":"Name001","data":[1,2,3]},
{"name":"Name002","data":[4,5,5]}
Each "Name" has its own ID, I would like in the "while" put each id with its own data.
Thank you for your help.
1. Update:
$id_name = "xx";
while($row = mysql_fetch_array($resultQuery)){
if($id !== $row['id']){
$response = array(
'name' => $row['name'],
'data' => array($row['data'])
);
$responses[] = $response;
}
$id_name = $row['id'];
}
echo json_encode($responses);
I am trying to check the ID first and if is different will bring to me:
[{"name":"Name001","data":["1"]},{"name":"Name002","data":["4"]}]
while($row = mysql_fetch_array($resultQuery))
$tmp[$row['name']][] = $row['data'];
foreach($tmp as $key => $item)
$responses[] = array('name' => $key, 'data' => $item);
echo json_encode($responses);
result
[{"name":"Name001","data":["1","2","3"]},{"name":"Name002","data":["4","5","6"]}]
I made a test here, hope you like.
$arr = array();
$arr[] = array(
'name' => 'val',
'data' => array(1, 2, 3)
);
$arr[] = array(
'name' => 'val',
'data' => array(1, 2, 3)
);
echo json_encode($arr);
Output:
[{"name":"val","data":[1,2,3]},{"name":"val","data":[1,2,3]}]

Need to merge many rows in one JSON

How to make from this:
{"color":[{"id":"41","name":"red"}]}
{"color":[{"id":"19","name":"blue"}]}
...
this thing by php and json_encode
{"color":[{"id":"41","name":"red"},{"id":"19","name":"blue"},...]}
Data come from DB and I use folowing code:
$json = array();
$jsonRow= array();
while ($row = mysqli_fetch_array($getResult)) {
$jsonRow = array(
$row['color'] => array(
array(
"id" => $row['id'],
"name" => $row['name'],
)
)
);
array_push($json,$jsonRow);
}
$arr= array();
$jsonRow= array();
while ($row = mysqli_fetch_array($getResult)) {
$jsonRow = array(
"id" => $row['id'],
"name" => $row['name'],
);
$arr[]=$jsonRow;
}
$json = json_encode(array('color'=>$arr));
echo $json;
How about this?
$json = array();
$colors= array();
while ($row = mysqli_fetch_array($getResult)) {
array_push($colors,
array(
"id" => $row['id'],
"name" => $row['name'],
)
);
$json = array( "colors" => $colors);
}
Try this:
$json = array();
$jsonRow= array();
while ($row = mysqli_fetch_array($getResult)) {
$jsonRow[] = array(
"id" => $row['id'],
"name" => $row['name'],
);
}
// color can be $row['color']
// If its constant, I am not sure if its a good idea to keep in the loop.
$json = array("color" => $jsonRow);
echo json_encode($json);

how to format json with array of json

I want output in json like this
response:{url="www.google.com/raj.png", size=12.344KB},{url="www.google.com/raj2.png", size=12.344KB},{url="www.google.com/raj4.png", size=12.344KB}
But currently i am getting
"url=> www.google.com/img1.png size => 12.344 KB,url=> www.google.com/img2.png size => 12.344 KB"
//Using some loop here
{
$response[] = array('url' => 'url_value','size' => 'file_size');
}
//without loop hardcoded values:
$response = array ( array('url' => "www.google.com/raj.png",'size' => "12.344KB"),
array('url' => "www.google.com/img2.png",'size' => "10.344KB") );
return json_encode($response);
$out= Array(
Array('url'=>'www.goog', 'size'=>'12KB'),
Array('url'=>'moogle', 'size'=>'13KB')
);
echo Json_encode($out);
Not sure how you are formatting your array, but if you define it like this it should do the trick:
$response_array = array(
'response' => array(
array(
'url' => 'www.google.com/raj.png',
'size' => '12.344KB',
),
array(
'url' => 'www.google.com/raj.png',
'size' => '12.344KB',
),
array(
'url' => 'www.google.com/raj.png',
'size' => '12.344KB',
),
),
);
$respose_json = json_encode($response_array);
echo($response_json);
For looping through some results, try something like this:
$response_array = array('response' => array());
foreach($result_set as $result_item) {
$response_item = array();
$response_item['url'] = $result_item['url'];
$response_item['size'] = $result_item['size'];
$response_array['response'][] = $response_item;
}
$respose_json = json_encode($response_array);
echo($response_json);
With the $result_set above being whatever data you've pulled from a DB, server, etc.

Simple question on foreach loop question with array with 2 variables. (with code)

How can I edit this foreach loop so that I will be able to use strpos to look if q is found in the label ?
The result array will contain those values.
$q may be anna or ann or reas john
<?php
$q = $_GET["q"];
if (!$q) return;
$data = Array(
Array(
'label' => 'anna c13',
'category' => 'Products'
),
Array(
'label' => 'anders andersson',
'category' => 'People'
),
Array(
'label' => 'andreas johnson',
'category' => 'People'
)
);
$result = array();
foreach ($data as $value) {
array_push($result, array(
"label" => $value["label"],
"category" => $value["category"]
));
}
$json = json_encode($result);
echo $json;
?>
This will output every array in $data where $q is somewhere in 'label'.
<?php
if( !isset( $_GET["q"] )) return;
$q = $_GET["q"];
$data = Array(
Array(
'label' => 'anna c13',
'category' => 'Products'
),
Array(
'label' => 'anders andersson',
'category' => 'People'
),
Array(
'label' => 'andreas johnson',
'category' => 'People'
)
);
$result = array();
foreach ($data as $value) {
if( strpos( $value['label'], $q ) !== false ) {
$result[] = $value;
}
}
$json = json_encode($result);
echo $json;
?>
You haven't defined keys for your $data array - so it automatically take the form of:
array(
0=>array(...),
1=>array(...),
2=>array(...)
)
This means that you're using strtolower on an int - so that's probably why it's failing.
foreach ($data as $value) {
if(strpos($value['label'], $q) !== false){
$result[] = $value;
}
}

Categories