array structure help in php to generate json - php

Fetching data from mySQL and Generating json through php like this:
while ($row = mysql_fetch_array($result)) {
$menu[] = array("type"=>"FeatureCollection",
"features" => [array("type"=>"Feature",
"geometry"=>array("type"=>"Point",
"coordinates"=>[-77.034084142948,38.909671288923]),
"properties"=>array("phone"=>"041","city"=>"Faisalabad","country"=>"Pakistan"))]
);
} $json = json_encode($menu);
echo $json;
and the result looks like this:
[
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-77.034084142948,
38.909671288923
]
},
"properties": {
"phone": "041",
"city": "Faisalabad",
"country": "Pakistan"
}
}
]
},
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-77.034084142948,
38.909671288923
]
},
"properties": {
"phone": "041",
"city": "Faisalabad",
"country": "Pakistan"
}
}
]
},
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-77.034084142948,
38.909671288923
]
},
"properties": {
"phone": "041",
"city": "Faisalabad",
"country": "Pakistan"
}
}
]
}]
As you can see {"type": "FeatureCollection","features":[{...}]}is being repeated three time, I want it to appear at the beginning only like following json, so that {"type": "FeatureCollection","features":[{I WANT LOOP HERE only}]}: I want this result:
[
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-77.034084142948,
38.909671288923
]
},
"properties": {
"phone": "041",
"city": "Faisalabad",
"country": "Pakistan"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-77.034084142948,
38.909671288923
]
},
"properties": {
"phone": "041",
"city": "Faisalabad",
"country": "Pakistan"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-77.034084142948,
38.909671288923
]
},
"properties": {
"phone": "041",
"city": "Faisalabad",
"country": "Pakistan"
}
}
]
}]
Please help I have been trying too much. Thanks

Well, why do you keep repeating "FeatureCollection" in the while-loop if you only want it once?
I think you want something like this:
$menu = array();
features = array();
$menu['type'] = 'FeatureCollection';
while ($row = mysql_fetch_array($result)) {
$features[] = array("type"=>"Feature",
//Other features here
);
}
$menu['features'] = $features;

In your $menu array you shoud define key type with value FeatureCollection, and key features. And in your while loop do this:
$menu = array(
'type' => 'FeatureCollection',
'features' => array()
);
while ($row = mysql_fetch_array($result)) {
$menu[`features`][] = array(...);
}

Related

Elasticsearch batch not indexing all items using PHP client

I've been trying to build an index in ES and add the initial items to it (around 350k), using PHP.
I tried all kinds of batch sizes (from 10 items to 1k), check the count, check the threshold, but for some reason it doesn't index every item.
It just skips over some random items, without any errors in the batch result response. I feel like I tried everything and I have to idea what to do next
I'm using Amazon OpenSearch with the latest supported ES (7.10).
The index looks like this:
{
"wonder-search": {
"aliases": {},
"mappings": {
"properties": {
"address": {
"type": "text"
},
"city": {
"type": "text"
},
"city_id": {
"type": "integer"
},
"duration": {
"type": "integer"
},
"filename": {
"type": "text"
},
"geo_point": {
"type": "geo_point"
},
"icon": {
"type": "keyword"
},
"is_sandbox": {
"type": "integer"
},
"item_id": {
"type": "integer"
},
"item_label": {
"type": "keyword"
},
"latitude": {
"type": "float"
},
"longitude": {
"type": "float"
},
"search_text_caption_json": {
"type": "text",
"index_phrases": true
},
"search_text_city_json": {
"type": "text",
"index_phrases": true
},
"search_text_completion": {
"type": "completion",
"analyzer": "simple",
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50,
"contexts": [
{
"name": "type",
"type": "CATEGORY"
}
]
},
"search_text_country_json": {
"type": "text",
"index_phrases": true
},
"search_text_cuisine_name_json": {
"type": "text",
"index_phrases": true
},
"search_text_location_name_json": {
"type": "text",
"index_phrases": true
},
"search_text_state_json": {
"type": "text",
"index_phrases": true
},
"search_text_tag_name_json": {
"type": "text",
"index_phrases": true
},
"search_text_username_json": {
"type": "text",
"index_phrases": true
},
"sort": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"sort_score": {
"type": "double"
},
"type": {
"type": "text"
},
"user_icon": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user_id": {
"type": "integer"
},
"username": {
"type": "keyword"
},
"vanity_url": {
"type": "keyword"
},
"video_count": {
"type": "integer"
}
}
},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"mapping": {
"ignore_malformed": "true"
},
"number_of_shards": "1",
"provided_name": "wonder-search",
"creation_date": "1671003076106",
"number_of_replicas": "1",
"uuid": "YQh1q40WTneLE4MWDWhArw",
"version": {
"created": "7100199"
}
}
}
}
}
and one item looks like this:
{
"_index": "wonder-search",
"_type": "_doc",
"_id": "wq2LD4UBUAuy7FQPhtZh",
"_version": 1,
"_seq_no": 2003,
"_primary_term": 1,
"found": true,
"_source": {
"sort": "4004",
"item_id": "4934",
"user_id": "434",
"user_icon": "/site-content/avatars/Sp8AXjTJvMbRao2oZbuiUuSVH042-1597776099045.jpeg",
"username": "chuurros",
"item_label": "Kyoto Katsugyu【京都勝牛】",
"search_text_username_json": [
"chuurros"
],
"search_text_caption_json": [
"Absolutely love their gyukatsu (beef katsu) here! Delicious and will keep you wanting more! 🥰"
],
"search_text_city_json": [
"Toronto"
],
"search_text_state_json": [
"Ontario"
],
"search_text_country_json": [
"Canada"
],
"search_text_location_name_json": [
"Kyoto Katsugyu【京都勝牛】"
],
"search_text_tag_name_json": [
"japanese",
"restaurant",
"asian",
"dining",
"topcollection-4934"
],
"search_text_cuisine_name_json": [],
"type": "video",
"vanity_url": "",
"icon": "",
"city": "Toronto",
"city_id": "439",
"latitude": "43.65682410",
"longitude": "-79.37617410",
"address": "134 Dundas St E",
"duration": "9.57",
"video_count": "0",
"sort_score": "43",
"filename": "373d75fd-4292-4e5b-a239-4b1c39ffc86c.MOV",
"is_sandbox": "0",
"geo_point": {
"lat": "43.65682410",
"lon": "-79.37617410"
},
"search_text_completion": {
"input": [
"Kyoto Katsugyu【京都勝牛】"
],
"contexts": {
"type": [
"video"
]
}
}
}
}
Any ideas why does it work like this?

Decoding JSON Mapbox

Hi I want to get the geometry value from JSON to array PHP
"type": "FeatureCollection",
"query": [
"maryland"
],
"features": [
{
"id": "address.2353009224859362",
"type": "Feature",
"place_type": [
"address"
],
"relevance": 1,
"properties": {
"accuracy": "street"
},
"text": "Maryland Drive",
"place_name": "Maryland Drive, Maryland New South Wales 2287, Australia",
"center": [
151.6501385,
-32.8792472
],
"geometry": {
"type": "Point",
"coordinates": [
151.6501385,
-32.8792472
]
},
what i can do right now just
$json1 = json_decode($json,true);
$json1['features']
the problem is I have tried to access
$json1['features][0]
but it won't give me any results
Is your Json Valid ?
As I took it from what you typed it is incomplete ...
I made changes to make it valid and now it's working well :
<?php
$json = '{
"type": "FeatureCollection",
"query": [
"maryland"
],
"features": [
{
"id": "address.2353009224859362",
"type": "Feature",
"place_type": [
"address"
],
"relevance": 1,
"properties": {
"accuracy": "street"
},
"text": "Maryland Drive",
"place_name": "Maryland Drive, Maryland New South Wales 2287, Australia",
"center": [
151.6501385,
-32.8792472
],
"geometry": {
"type": "Point",
"coordinates": [
151.6501385,
-32.8792472
]
}
}
]
}';
$data = json_decode($json,true);
print_r($data['features'][0]['geometry']['coordinates']);
Outputs :
Array (
[0] => 151.6501385
[1] => -32.8792472 )
$data = json_decode($json,true);
$coordinates = $data['features'][0]['geometry']['coordinates'];
//print_r($coordinates);
echo implode(' ', $coordinates);

How to validate required items of JSON SCHEMA array

I have a JSON Schema for new orders, that consists of order list and address.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"properties": {
"order": {
"type": "array",
"items": {
"type": "array",
"properties": {
"product_id": {
"type": "integer"
},
"quantity": {
"type": "integer"
}
},
"required": [
"product_id",
"quantity"
]
}
},
"address": {
"type": "array",
"properties": {
"name": {
"type": "string"
},
"phone": {
"type": "integer"
},
"address1": {
"type": "string"
},
"address2": {
"type": "string"
},
"city": {
"type": "string"
},
"state_or_region": {
"type": "string"
},
"country": {
"type": "string"
}
},
"required": [
"name",
"phone",
"address1",
"city",
"state_or_region",
"country"
]
}
},
"required": [
"order",
"address"
]
}
But it doesn't seem to actually validate the items at all (I'm using Laravel 5.2 with "justinrainbow/json-schema": "~2.0" ):
$refResolver = new \JsonSchema\RefResolver(new \JsonSchema\Uri\UriRetriever(), new \JsonSchema\Uri\UriResolver());
$schema = $refResolver->resolve(storage_path('schemas\orders.post.json'));
$errors = [];
$input = Request::input();
// Validate
$validator = new \JsonSchema\Validator();
$validator->check($input, $schema);
$msg = [];
if ($validator->isValid()) {
return Response::json(['valid'], 200, [], $this->pritify);
} else {
$msg['success'] = false;
$msg['message'] = "JSON does not validate";
foreach ($validator->getErrors() as $error) {
$msg['errors'][] = [
'error' => ($error['property'] = ' ') ? 'wrong_data' : $error['property'],
'message' => $error['message']
];
}
return Response::json($msg, 422, [], $this->pritify);
}
A request like this always comes valid:
{
"order": [
{
"product_id": 100,
"quantity": 1
},
{
"product_id": 0,
"quantity": 2
}
],
"address": []
}
Any ideas what am I doing wrong?
You have messed array and object types. The only array value in your scheme must be order. Fixed scheme:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"order": {
"type": "array",
"items": {
"type": "object",
"properties": {
"product_id": {
"type": "integer"
},
"quantity": {
"type": "integer"
}
},
"required": [
"product_id",
"quantity"
]
}
},
"address": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"phone": {
"type": "integer"
},
"address1": {
"type": "string"
},
"address2": {
"type": "string"
},
"city": {
"type": "string"
},
"state_or_region": {
"type": "string"
},
"country": {
"type": "string"
}
},
"required": [
"name",
"phone",
"address1",
"city",
"state_or_region",
"country"
]
}
},
"required": [
"order",
"address"
]
}
And validation errors I got with you test data:
JSON does not validate. Violations:
[address.name] The property name is required
[address.phone] The property phone is required
[address.address1] The property address1 is required
[address.city] The property city is required
[address.state_or_region] The property state_or_region is required
[address.country] The property country is required

Query JSON (GeoJSON ...), in PHP to extract value

I've this GeoJSON ....
{
"more": true,
"features": [{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[7.672117, 44.901697],
[7.672137, 44.901766],
[7.672167, 44.901828],
[7.672207, 44.901888]
],
"bbox": [7.671508, 44.901697, 7.676434, 44.912198]
},
"properties": {
"boundary": {
"min_lat": 44.901697,
"min_lon": 7.671508,
"max_lat": 44.912198,
"max_lon": 7.676434
},
"captured_at": 1432989312291,
"key": "Lm7zCv3niXy9jBDmaKEuzw",
"keys": ["O-UdnDpmS8_WTQgOqkj8_w", "BQUybUzc2liYTx5Rc6lEEA", "-n5Yw2WEgnLTVk4kVJbnGQ"]
}
}, {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[7.67173, 44.912223],
[7.671718, 44.912186],
[7.671685, 44.912138],
[7.671668, 44.912084]
],
"bbox": [7.671508, 44.911598, 7.67173, 44.912223]
},
"properties": {
"boundary": {
"min_lat": 44.911598,
"min_lon": 7.671508,
"max_lat": 44.912223,
"max_lon": 7.67173
},
"captured_at": 1432985665206,
"key": "gP_RMGgi8Vs26HEtuQBzBw",
"keys": ["0PZ_0b5gDwgv_wGR-PaH6g", "D6B1-IcUpWc6rEq2v-a4AQ", "L9uEPoXiSjagWY2hPTRpBg"]
}
}, {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[7.671187, 44.911639],
[7.671243, 44.911675],
[7.671249, 44.911742],
[7.671262, 44.911796]
],
"bbox": [7.671137, 44.911639, 7.671674, 44.912609]
},
"properties": {
"boundary": {
"min_lat": 44.911639,
"min_lon": 7.671137,
"max_lat": 44.912609,
"max_lon": 7.671674
},
"captured_at": 1433505642167,
"key": "wGJ8pn9A41vdyQN-WSIT_Q",
"keys": ["DVZQEFI_8qczLI99NCpDkQ", "edPjE41cA8h4HIzmbS0MyA", "JYghFOvUuPPtpQL5ff0lmQ"]
}
}, {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[7.671765, 44.912323],
[7.671756, 44.912292],
[7.671746, 44.912258],
[7.671734, 44.912223]
],
"bbox": [7.671481, 44.911138, 7.672658, 44.912323]
},
"properties": {
"boundary": {
"min_lat": 44.911138,
"min_lon": 7.671481,
"max_lat": 44.912323,
"max_lon": 7.672658
},
"captured_at": 1432743361672,
"key": "4x5ay3CHwgxFTdIIQg81-A",
"keys": ["lx30DGH6cFpa5VWD98pDDA", "xvJ2X2FeFfCDm2cVvPgS6A", "gBJFHuS19-2k9fs3_vJ8zQ"]
}
}],
"type": "FeatureCollection"
}
... and I need to search the value D6B1-IcUpWc6rEq2v-a4AQ and, when I've found it return the value of the "key" tag related, so in this case gP_RMGgi8Vs26HEtuQBzBw
Any suggestion / example for to do it in PHP?
Thank you very much in advance!!!
Cesare
You need to json_decode your string and then foreach decoded array.
I write function findKey. You need to pass two variables: your json string and key to search for
function findKey($geoJson, $key) {
$geoArray = json_decode($geoJson, true);
foreach ($geoArray['features'] as $geoFeature) {
if (in_array($key, $geoFeature['properties']['keys'])) {
return $geoFeature['properties']['key'];
}
}
}
This code example finds your desired key gP_RMGgi8Vs26HEtuQBzBw:
<?php
$geoJson = <<<EOF
{
"more": true,
"features": [{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[7.672117, 44.901697],
[7.672137, 44.901766],
[7.672167, 44.901828],
[7.672207, 44.901888]
],
"bbox": [7.671508, 44.901697, 7.676434, 44.912198]
},
"properties": {
"boundary": {
"min_lat": 44.901697,
"min_lon": 7.671508,
"max_lat": 44.912198,
"max_lon": 7.676434
},
"captured_at": 1432989312291,
"key": "Lm7zCv3niXy9jBDmaKEuzw",
"keys": ["O-UdnDpmS8_WTQgOqkj8_w", "BQUybUzc2liYTx5Rc6lEEA", "-n5Yw2WEgnLTVk4kVJbnGQ"]
}
}, {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[7.67173, 44.912223],
[7.671718, 44.912186],
[7.671685, 44.912138],
[7.671668, 44.912084]
],
"bbox": [7.671508, 44.911598, 7.67173, 44.912223]
},
"properties": {
"boundary": {
"min_lat": 44.911598,
"min_lon": 7.671508,
"max_lat": 44.912223,
"max_lon": 7.67173
},
"captured_at": 1432985665206,
"key": "gP_RMGgi8Vs26HEtuQBzBw",
"keys": ["0PZ_0b5gDwgv_wGR-PaH6g", "D6B1-IcUpWc6rEq2v-a4AQ", "L9uEPoXiSjagWY2hPTRpBg"]
}
}, {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[7.671187, 44.911639],
[7.671243, 44.911675],
[7.671249, 44.911742],
[7.671262, 44.911796]
],
"bbox": [7.671137, 44.911639, 7.671674, 44.912609]
},
"properties": {
"boundary": {
"min_lat": 44.911639,
"min_lon": 7.671137,
"max_lat": 44.912609,
"max_lon": 7.671674
},
"captured_at": 1433505642167,
"key": "wGJ8pn9A41vdyQN-WSIT_Q",
"keys": ["DVZQEFI_8qczLI99NCpDkQ", "edPjE41cA8h4HIzmbS0MyA", "JYghFOvUuPPtpQL5ff0lmQ"]
}
}, {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[7.671765, 44.912323],
[7.671756, 44.912292],
[7.671746, 44.912258],
[7.671734, 44.912223]
],
"bbox": [7.671481, 44.911138, 7.672658, 44.912323]
},
"properties": {
"boundary": {
"min_lat": 44.911138,
"min_lon": 7.671481,
"max_lat": 44.912323,
"max_lon": 7.672658
},
"captured_at": 1432743361672,
"key": "4x5ay3CHwgxFTdIIQg81-A",
"keys": ["lx30DGH6cFpa5VWD98pDDA", "xvJ2X2FeFfCDm2cVvPgS6A", "gBJFHuS19-2k9fs3_vJ8zQ"]
}
}],
"type": "FeatureCollection"
}
EOF;
$key = 'D6B1-IcUpWc6rEq2v-a4AQ';
function findKey($geoJson, $key) {
$geoArray = json_decode($geoJson, true);
foreach ($geoArray['features'] as $geoFeature) {
if (in_array($key, $geoFeature['properties']['keys'])) {
return $geoFeature['properties']['key'];
}
}
}
var_dump(findKey($geoJson,$key));

I want this json to be displayed in angular

I want this multilevel array of jason in angular format i am not being able to do it.
Json file to be displayed in angular goes here.
I am able to display properties and geometry not others
{
"type": "FeatureCollection", //first fie
"metadata": {
"generated": 1456209730000,
"api": "1.4.0",
},
"features": [
{
"type": "Feature",
"properties": {
"mag": 2.3,
},
"geometry": {
"type": "Point",
"coordinates": [
-150.7703,
63.5849
]},
"id": "ak12869032"
},
{
"type": "Feature",
"properties": {
"mag": 2.43
},
"geometry": {
"type": "Point",
"coordinates": [
-120.8253326,
36.6078339
]},
"id": "nc72596150"
}],
"bbox": [ //last field
-150.7703,
33.7698333
]}
Please follow these steps:
1) create $scope.tempData variable in your controller
2) get your value in $scope
3) $scope.tempData=[your json data];
4) you can use tempData in anywhere within controller.
for(var d in tempData)
{
var data=tempData[d].id;
}
now you are able to getting id from your json.but for its giving last value.
so you can cretae array and pushed on every loop.
5) if you want use in html can use ng-repeat.
If you are beginner vist my blog.
Edit your json:
{
"type": "FeatureCollection",
"metadata": {
"generated": 1456209730000,
"api": "1.4.0",
"count": 3
},
"features": [
{
"type": "Feature",
"properties": {
"mag": 2.3,
"place": "93km WNW of Cantwell, Alaska",
"time": 1456207082000
},
"geometry": {
"type": "Point",
"coordinates": [
-150.7703,
63.5849
]
},
"id": "ak12869032"
},
{
"type": "Feature",
"properties": {
"mag": 2.43,
"place": "42km WSW of Mendota, California",
"time": 1456206494170
},
"geometry": {
"type": "Point",
"coordinates": [
-120.8253326,
36.6078339
]
},
"id": "nc72596150"
}
],
"bbox": [
-150.7703,
33.7698333,
0.79,
-115.9471667,
63.5849,
8.8
]
}
See the points: http://bl.ocks.org/d/60bc280edef4ed4da464
To display json in angularjs, please use filter json with model.
<input type="text" ng-model="(jsonData | json)">{{jsonData | json}}</input>
Using Pre Tag we can style it in html page
In your Controller:
$scope.details={name:"xyz",address:[{place:"cc"},{place:"second"}]}
In HTML Page: <pre>{{details|json}}</pre>

Categories