Decoding JSON Mapbox - php

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);

Related

Giving priority to prefix match in elasticsearch in php

Is there a way in elasticsearch to give more priority for the prefix match than to the string that contains that word?
For ex.- priorities of words if I search for ram should be like this:
Ram Reddy
Joy Ram Das
Kiran Ram Goel
Swati Ram Goel
Ramesh Singh
I have tried mapping as given in here.
I have done like this:
$params = [
"index" => $myIndex,
"body" => [
"settings"=> [
"analysis"=> [
"analyzer"=> [
"start_with_analyzer"=> [
"tokenizer"=> "my_edge_ngram",
"filter"=> [
"lowercase"
]
]
],
"tokenizer"=> [
"my_edge_ngram"=> [
"type"=> "edge_ngram",
"min_gram"=> 3,
"max_gram"=> 15
]
]
]
],
"mappings"=> [
"doc"=> [
"properties"=> [
"label"=> [
"type"=> "text",
"fields"=> [
"keyword"=> [
"type"=> "keyword"
],
"ngramed"=> [
"type"=> "text",
"analyzer"=> "start_with_analyzer"
]
]
]
]
]
]
]
];
$response = $client->indices()->create($params); // create an index
and searching like this:
$body = [
"size" => 100,
'_source' => $select,
"query"=> [
"bool"=> [
"should"=> [
[
"query_string"=> [
"query"=> "ram*",
"fields"=> [
"value"
],
"boost"=> 5
]
],
[
"query_string"=> [
"query"=> "ram*",
"fields"=> [
"value.ngramed"
],
"analyzer"=> "start_with_analyzer",
"boost"=> 2
]
]
],
"minimum_should_match"=> 1
]
]
];
$params = [
'index' => $myIndex,
'type' => $myType,
'body' => []
];
$params['body'] = $body;
$response = $client->search($params);
The json of query is as follows:
{
"size": 100,
"_source": [
"label",
"value",
"type",
"sr"
],
"query": {
"bool": {
"should": [
{
"query_string": {
"query": "ram*",
"fields": [
"value"
],
"boost": 5
}
},
{
"query_string": {
"query": "ram*",
"fields": [
"value.ngramed"
],
"analyzer": "start_with_analyzer",
"boost": 2
}
}
],
"minimum_should_match": 1,
"must_not": {
"match_phrase": {
"type": "propertyValue"
}
}
}
}
}
I am using elasticsearch 5.3.2
Is there any other way to sort the results for the search in the relational database using the search method in php?
You should not enable fielddata unless really required. To overcome this you can use sub field.
Make the following changes to your code:
"label"=>[
"type"=>"text",
//"fielddata"=> true, ---->remove/comment this line
"analyzer"=>"whitespace",
"fields"=>[
"keyword"=>[
"type"=>"keyword"
]
]
]
To sort on type field use type.keyword instead. This change apply to any field of text type and has a sub-field of type keyword available (assuming the name of this field is keyword). So change as below:
'sort' => [
["type.keyword"=>["order"=>"asc"]],
["sr"=>["order"=>"asc"]],
["propLabels"=>["order"=>"asc"]],
["value"=>["order"=>"asc"]]
]
Update : Index creation and query to get desired output
Create the index as below:
{
"settings": {
"analysis": {
"analyzer": {
"start_with_analyzer": {
"tokenizer": "my_edge_ngram",
"filter": [
"lowercase"
]
}
},
"tokenizer": {
"my_edge_ngram": {
"type": "edge_ngram",
"min_gram": 3,
"max_gram": 15
}
}
}
},
"mappings": {
"_doc": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
},
"ngramed": {
"type": "text"
}
}
}
}
}
}
}
Use the query below to get the desired result:
{
"query": {
"bool": {
"should": [
{
"query_string": {
"query": "Ram",
"fields": [
"name"
],
"boost": 5
}
},
{
"query_string": {
"query": "Ram",
"fields": [
"name.ngramed"
],
"analyzer": "start_with_analyzer",
"boost": 2
}
}
],
"minimum_should_match": 1
}
}
}
In the above the query with boost value 5 increases the score for those documents where Ram is present in name. The other query with boost 2 further increases the score for the documents where name starts with Ram.
Sample O/P:
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "2",
"_score": 2.0137746,
"_source": {
"name": "Ram Reddy"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_score": 1.4384104,
"_source": {
"name": "Joy Ram Das"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "3",
"_score": 0.5753642,
"_source": {
"name": "Ramesh Singh"
}
}
]

ElasticSearch query with diacritics / accents in PHP

I have the following expression: "noapte bună" and I'm trying to get the same result when I'm searching for "bună" or "buna".
I have followed to tutorial here : https://www.elastic.co/guide/en/elasticsearch/guide/current/asciifolding-token-filter.html but to no result.
This is my code:
$params = ['index' => 'asciiv3', 'body' => [
"settings" => [
"analysis" => [
"analyzer" => [
"folding" => [
"tokenizer" => "standard",
"filter" => [ "lowercase", "asciifolding" ]
]
]
]
],
"mappings" => [
"asciiv3" => [
"properties" => [
"saying" => [
"type" => "string",
"analyzer" => "standard",
"fields" => [
"folded" => [
"type" => "string",
"analyzer" => "folding"
]
]
]
]
]
]
]];
self::$instance->indices()->create($params);
and this is the query array:
'multi_match' =>
array(
"type" => "most_fields",
"query" => "bună",
"fields" => [ "saying", "saying.folded" ]
)
Does anyone know what I'm doing wrong?
It works for me. This is my setup:
PUT asciiv3
{
"settings": {
"analysis": {
"analyzer": {
"folding": {
"tokenizer": "standard",
"filter": [
"lowercase",
"asciifolding"
]
}
}
}
},
"mappings": {
"asciiv3": {
"properties": {
"saying": {
"type": "string",
"analyzer": "standard",
"fields": {
"folded": {
"type": "string",
"analyzer": "folding"
}
}
}
}
}
}
}
POST /asciiv3/asciiv3/1
{
"saying":"bună ziua"
}
POST /asciiv3/asciiv3/2
{
"saying":"buna ziua"
}
GET /asciiv3/_search
{
"query": {
"multi_match": {
"type": "most_fields",
"query": "bună",
"fields": [
"saying",
"saying.folded"
]
}
}
}
With these results:
"hits": {
"total": 2,
"max_score": 0.2712221,
"hits": [
{
"_index": "asciiv3",
"_type": "asciiv3",
"_id": "1",
"_score": 0.2712221,
"_source": {
"saying": "bună ziua"
}
},
{
"_index": "asciiv3",
"_type": "asciiv3",
"_id": "2",
"_score": 0.028130025,
"_source": {
"saying": "buna ziua"
}
}
]
}

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>

How do you access json nested elements with php

For 24hrs I have been really struggling to find how to access the email and address fields. I think I've tried every combination there is
my last attempt
$obj = json_decode($json);
$emails = $obj->emails->address;
JSON OUTPUT
{
"#http_status_code": 200,
"#visible_sources": 1,
"#available_sources": 1,
"#search_id": "1507310336407868146373527620323253083",
"query": {
"emails": [
{
"address": "paulsparktest#googlemail.com",
"address_md5": "d92590f691eab834586686c563567382"
}
]
},
"possible_persons": [
{
"#search_pointer": "3984f5b332b6efb3331bb137e1e97829ddff0971d9de9347cbd7fb8f82dc68de093a525a66584694339bfe580baff2aacb77954e0f154a1d0bd0a36588094972a72c1c4a63197a9736f6c425afdf66e5d8e52d35073d6499036efe9a234dd1d886f71bf54b9911a19725f118b6cd7bca521c246fe3b890a957596f8236c3c4ac5ba241198c3bdfa2f44a4e361393f1bf407130ffb5b9e2f6b1ccffca87befd0b147e51a12a54773ca31fc1a364b8cde876ca5f42b5d6f0c319f18300cab29fc1",
"names": [
{
"first": "Paul",
"last": "Johnson",
"display": "Paul Johnson"
}
],
"usernames": [
{
"content": "pauljohnson111"
}
],
"gender": {
"#inferred": true,
"content": "male"
},
"addresses": [
{
"country": "GB",
"state": "ENG",
"city": "London",
"display": "London, England"
}
],
"user_ids": [
{
"content": "374967130#twitter"
}
],
"images": [
{
"url": "http://pbs.twimg.com/profile_images/1546592694/Roccc.jpg",
"thumbnail_token": "AE2861B242686E7DDBDF0D814A3486E1D19BE9609F41B4AA71B6D0FEB03454A84C36C69AC788EF676B93C5274D29CD76361050E1F057871D&dsid=39844"
}
]
}
]
}
This will get the address for you
$obj->query->emails[0]->address;
if you want to iterate and get other addresses
foreach ( $obj->query->emails as $email ) {
echo $email->address;
}
You can get address & name via follow code.
// Getting email
echo $data->query->emails[0]->address;
// Getting name
echo $data->possible_persons[0]->names[0]->display;
Full code
$data = '{
"#http_status_code": 200,
"#visible_sources": 1,
"#available_sources": 1,
"#search_id": "1507310336407868146373527620323253083",
"query": {
"emails": [
{
"address": "paulsparktest#googlemail.com",
"address_md5": "d92590f691eab834586686c563567382"
}
]
},
"possible_persons": [
{
"#search_pointer": "3984f5b332b6efb3331bb137e1e97829ddff0971d9de9347cbd7fb8f82dc68de093a525a66584694339bfe580baff2aacb77954e0f154a1d0bd0a36588094972a72c1c4a63197a9736f6c425afdf66e5d8e52d35073d6499036efe9a234dd1d886f71bf54b9911a19725f118b6cd7bca521c246fe3b890a957596f8236c3c4ac5ba241198c3bdfa2f44a4e361393f1bf407130ffb5b9e2f6b1ccffca87befd0b147e51a12a54773ca31fc1a364b8cde876ca5f42b5d6f0c319f18300cab29fc1",
"names": [
{
"first": "Paul",
"last": "Johnson",
"display": "Paul Johnson"
}
],
"usernames": [
{
"content": "pauljohnson111"
}
],
"gender": {
"#inferred": true,
"content": "male"
},
"addresses": [
{
"country": "GB",
"state": "ENG",
"city": "London",
"display": "London, England"
}
],
"user_ids": [
{
"content": "374967130#twitter"
}
],
"images": [
{
"url": "http://pbs.twimg.com/profile_images/1546592694/Roccc.jpg",
"thumbnail_token": "AE2861B242686E7DDBDF0D814A3486E1D19BE9609F41B4AA71B6D0FEB03454A84C36C69AC788EF676B93C5274D29CD76361050E1F057871D&dsid=39844"
}
]
}
]
}';
$data = json_decode($data);
// Getting email
echo $data->query->emails[0]->address;
// Getting name
echo $data->possible_persons[0]->names[0]->display;

array structure help in php to generate json

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(...);
}

Categories