PHP format mysql result into json - php

I've been beating my head all day because of this.
How to convert the results from the image above into json format like this
{
"name": "João Batista Da Silva Júnior",
"y": "9.83",
"drilldown": "João Batista Da Silva Júnior",
"data": [
[
"assiduidade",
10
],
[
"normas",
9
],
[
"eficiencia",
10
],
[
"relacionamento",
10
],
[
"iniciativa",
10
],
[
"visao",
10
]
]
}
]
}
I'm trying this code but it doesn't reproduce the format required.
$info["aval"] = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$info2 = ['name' => $auxiliar, 'y' => $media, 'drilldown' => $auxiliar, 'data' => [[$topico, floatval($nota)]]];
$info["aval"][] = $info2;
}

Related

How to format JSON object PHP

I am trying to create a POST to a REST API to create a new object. I cannot figure out how to properly format my JSON.
Here's the response from the GET of an existing object:
{
"name": "product 2 mem"
"type": "simple"
"categories": array:1 [▼
0 => {
"id": 75
}
]
"meta_data": array:1 [▼
"id": 3665
"key": "_yith_wcbm_product_meta"
"value": {
"id_badge": "2955"
}
}
]
}
Here is the POST I'm trying to create:
$data = [
'name' => 'product name',
'type' => 'simple',
'categories' => [
[
'id' => 75
],
'meta_data' => [
'_yith_wcbm_product_meta' => [
'id_badge' => '2955'
]
]
];
You got typo in you json data.
$response = '{
"id": 3665,
"key": "_yith_wcbm_product_meta",
"value": {
"id_badge": "2955"
}
}';
$array = json_decode($response,true);
$return = ['meta_data'=>['key'=>$array['key'],'value'=>$array['value']]];
echo json_encode($return);
I figured out how to format it:
'meta_data' => [
[
'key' => '_yith_wcbm_product_meta',
'value' => ['id_badge' => '2955']
]

what is the equivalent query from mysql to mongosb

im new to mongodb after years with mysql and trying to figure this equivalent query in mongodb with php composer
select * from table where (x > 0 and x < 30) or x = 'half' and sid = 1
$query = [
'$and' => [
[
'sid'=> 1
], ['$and' => [[
'info.x' => [
'$lt' => '30'
]
], [
'info.x' => [
'$gt' => '0'
]
], [
'info.x' => [ /// i want to put this in $or
'half'
]
]]
]
]
];
i want to display all what is greater then 0 and less then 30 or equal to 'half' where sid is 1
thanks
It must be
db.collection.find({
"$or": [
{ "x": { "$gte": 0, "$lte": 30 }},
{ "x": "half" }
],
"sid": 1
})
Or
$query = [
'$or'=> [
[ 'x'=> [ '$gte'=> 0, '$lte'=> 30 ]],
[ 'x'=> 'half' ]
],
'sid'=> 1
]

How do I extract subdocument in laravel mongodb

Hello Good Developers,
I am using jenssegers/laravel-mongodb package to query my MongoDB from Laravel.
Here's Fiddle for my query: https://mongoplayground.net/p/qzbNN8Siy-3
I have following JSON
[{
"id": "GLOBAL_EDUCATION",
"general_name": "GLOBAL_EDUCATION",
"display_name": "GLOBAL_EDUCATION",
"profile_section_id": 0,
"translated": [
{
"con_lang": "US-EN",
"country_code": "US",
"language_code": "EN",
"text": "What is the highest level of education you have completed?",
"hint": null
},
{
"con_lang": "US-ES",
"country_code": "US",
"language_code": "ES",
"text": "\u00bfCu\u00e1l es su nivel de educaci\u00f3n?",
"hint": null
}...
{
....
}
]
I am trying to run following command
db.collection.find({ 'id': "GLOBAL_EDUCATION" },{_id:0, id:1, general_name:1, translated:{ $elemMatch: {con_lang: "US-EN"} }})
Expecting result like this
[
{
"general_name": "GLOBAL_EDUCATION",
"id": "GLOBAL_EDUCATION",
"translated": [
{
"con_lang": "US-EN",
"country_code": "US",
"hint": null,
"language_code": "EN",
"text": "What is the highest level of education you have completed?"
}
]
}
]
Everything is fine while query directly in MoDB but issue arise when I am trying this in Laravel.
I've tried every possible known function from MongoDB package. but Not able to do this.
here's my Array
$findArray = [
[
'id' => "GLOBAL_EDUCATION",
],
[
'_id' => 0,
'id' => 1,
'general_name' => 1,
'translated' => [
'$elemMatch' => ['con_lang' => "US-EN"]
],
]
];
$model = GlobalQuestions::raw()->find($findArray) //OR
$data = GlobalQuestions::raw(function($collection) use ($findArray){
return $collection->find($findArray);
});
What I am doing wrong here, is this kind of Find() not possible here and I've to do this by aggregation?
Since no-one answered this, I am posting the solution if someone is having the same issue.
Doing some more R&D on the same I was able to do this using where and Project as well by Aggregation Pipelines.
----- Using Where() and Project() ------
$projectArray = [
'_id' => 0,
'id' => 1,
'general_name' => 1,
'translated' => [
'$elemMatch' => ['con_lang' => "FR-FR"]
],
];
$data = GlobalQuestions::where('id', '=', 'GLOBAL_EDUCATION')
->project($projectArray)
->get();
--- Using Aggregation and $unwind ---
$data = GlobalQuestions::raw(function($collection) {
return $collection->aggregate([
[
'$match' => [
'id' => "GLOBAL_EDUCATION"
]
],
[
'$unwind' => '$translated',
],
[
'$match' => [
'translated.con_lang' => "US-EN"
]
],
[
'$project' => [
'_id'=> 0,
'id'=> 1,
'general_name' => 1,
'translated' => 1,
]
]
]);
})->first();

Sending Json with Guzzle

I need to send Json with one of the parameters being an array . The following code works perfectly .
$html = $client->post($url,
['json'=>[ 'requestData'=>
[
'sessionID'=>'261-7306141-0539957'
]
,
'productTargets'=>
[
[ 'ProductID'=>$data[0] ],
[ 'ProductID'=>$data[1] ],
[ 'ProductID'=>$data[2] ],
[ 'ProductID'=>$data[3] ],
[ 'ProductID'=>$data[4] ],
],
] ]
);
As you can see I am manually entering data[0],data[1],data[2] in productTargets . I need to send multiple like 100's of
'ProductID' object . I tried using
json_encode( [ [ 'ProductID'=>$data[0] ],
['ProductID'=>$data[2] ],
['ProductID'=>$data[3] ],
['ProductID'=>$data[4] ], ]
)
But it's not working . How do I send the data
Perhaps an approach like this:
<?php
$data =
[
'json' =>
[
'requestData'=>
[
'sessionID'=>'261-7306141-0539957'
]
]
];
$ids = range(1, 4); // Some dummy ids.
shuffle($ids);
$product_ids = array_map(function($v) {
return ['ProductID' => $v];
}, $ids);
$data['json']['productTargets'] = $product_ids;
var_dump(json_encode($data, JSON_PRETTY_PRINT));
Example output:
string(383) "{
"json": {
"requestData": {
"sessionID": "261-7306141-0539957"
},
"productTargets": [
{
"ProductID": 4
},
{
"ProductID": 3
},
{
"ProductID": 1
},
{
"ProductID": 2
}
]
}
}"

Extract Json php MultilineString coordinates from Mysql

How do I insert and extract the following from a MySQL database using php. I have tried 'Multiploygon, Multilinestring, GeometryCollection' but cannot get the correct output as below.
1) JSON file to insert
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "GID": 4728339, "PRCL_KEY": "0000T0JT005300000056000010", "PRCL_TYPE": "E", "LSTATUS": "R", "WSTATUS": "C", "GEOM_AREA": 1558.723715, "COMMENTS": "", "TAG_X": 30.920674, "TAG_Y": -25.452585, "TAG_VALUE": "1\/56", "TAG_SIZE": 0.000020, "TAG_ANGLE": 6.282645, "TAG_JUST": "MC", "ID": "T0JT00530000005600001", "DATE_STAMP": "2010\/10\/05" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 30.920838, -25.452746 ], [ 30.920876, -25.452431 ], [ 30.920876, -25.452431 ], [ 30.921498, -25.452367 ], [ 30.921498, -25.452367 ], [ 30.921492, -25.452341 ], [ 30.921492, -25.452341 ], [ 30.920476, -25.452445 ], [ 30.920476, -25.452445 ], [ 30.920441, -25.452719 ], [ 30.920441, -25.452719 ], [ 30.920838, -25.452746 ], [ 30.920838, -25.452746 ] ] ] } }
,
{ "type": "Feature", "properties": { "GID": 4822420, "PRCL_KEY": "0000T0JU004000004445000000", "PRCL_TYPE": "E", "LSTATUS": "R", "WSTATUS": "C", "GEOM_AREA": 391.799538, "COMMENTS": "", "TAG_X": 31.337096, "TAG_Y": -25.508292, "TAG_VALUE": "4445", "TAG_SIZE": 0.000020, "TAG_ANGLE": 0.002299, "TAG_JUST": "MC", "ID": "T0JU00400000444500000", "DATE_STAMP": "2013\/05\/22" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 31.336972, -25.508217 ], [ 31.337103, -25.508419 ], [ 31.337103, -25.508419 ], [ 31.337234, -25.508350 ], [ 31.337234, -25.508350 ], [ 31.337101, -25.508149 ], [ 31.337101, -25.508149 ], [ 31.336972, -25.508217 ], [ 31.336972, -25.508217 ] ] ] } }
2) Output needed
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000007100000"},"type":"Polyline","coordinates":[29.245262,-26.95622,29.245272,-26.956412,29.245272,-26.956412,29.245282,-26.956603,29.245282,-26.956603,29.245826,-26.956581,29.245826,-26.956581,29.246128,-26.956398,29.246128,-26.956398]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000007300000"},"type":"Polyline","coordinates":[29.245211,-26.955206,29.245231,-26.955589,29.245231,-26.955589,29.246086,-26.955554,29.246086,-26.955554,29.246066,-26.95517,29.246066,-26.95517,29.245211,-26.955206,29.245211,-26.955206]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000007500000"},"type":"Polyline","coordinates":[29.245171,-26.954439,29.245191,-26.954822,29.245191,-26.954822,29.246047,-26.954787,29.246047,-26.954787,29.246037,-26.954594,29.246037,-26.954594,29.245558,-26.954614,29.245558,-26.954614]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000010200000"},"type":"Polyline","coordinates":[29.2436,-26.954444,29.243605,-26.95454,29.243605,-26.95454,29.243623,-26.954887,29.243623,-26.954887,29.244051,-26.95487,29.244051,-26.95487,29.244028,-26.954427,29.244028,-26.954427]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000030400000"},"type":"Polyline","coordinates":[29.242942,-26.949925,29.242963,-26.950314,29.242963,-26.950314,29.243388,-26.950298,29.243388,-26.950298,29.243374,-26.950036,29.243374,-26.950036,29.243367,-26.949915,29.243367,-26.949915]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000010400001"},"type":"Polyline","coordinates":[29.243429,-26.951122,29.243439,-26.951314,29.243439,-26.951314,29.243867,-26.951296,29.243867,-26.951296,29.243857,-26.951105,29.243857,-26.951105,29.243477,-26.95112,29.243477,-26.95112]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000010500000"},"type":"Polyline","coordinates":[29.243288,-26.956694,29.243309,-26.957098,29.243309,-26.957098,29.243328,-26.95746,29.243328,-26.95746,29.243755,-26.957443,29.243755,-26.957443,29.243716,-26.956676,29.243716,-26.956676]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000010600000"},"type":"Polyline","coordinates":[29.243261,-26.956168,29.243288,-26.956694,29.243288,-26.956694,29.243716,-26.956676,29.243716,-26.956676,29.243697,-26.956312,29.243697,-26.956312,29.243688,-26.956151,29.243688,-26.956151]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000010800000"},"type":"Polyline","coordinates":[29.243178,-26.954558,29.243195,-26.954905,29.243195,-26.954905,29.243304,-26.954901,29.243304,-26.954901,29.243623,-26.954887,29.243623,-26.954887,29.243605,-26.95454,29.243605,-26.95454]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000009400000"},"type":"Polyline","coordinates":[29.244647,-26.952287,29.244653,-26.952395,29.244653,-26.952395,29.244682,-26.952394,29.244682,-26.952394,29.244697,-26.952669,29.244697,-26.952669,29.245078,-26.952654,29.245078,-26.952654]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000009500000"},"type":"Polyline","coordinates":[29.244599,-26.95165,29.244609,-26.951846,29.244609,-26.951846,29.244652,-26.951844,29.244652,-26.951844,29.244661,-26.952032,29.244661,-26.952032,29.244825,-26.952025,29.244825,-26.952025]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000009800000"},"type":"Polyline","coordinates":[29.244753,-26.950491,29.244773,-26.950875,29.244773,-26.950875,29.244987,-26.950866,29.244987,-26.950866,29.244967,-26.950483,29.244967,-26.950483,29.244753,-26.950491,29.244753,-26.950491]}}
3) Current PHP code
$geojson = array( 'type' => 'FeatureCollection', 'features' => array() );
while($row = mysql_fetch_assoc($dbquery)) {
$feature = array(
'type' => 'Feature',
'geometry' => array(
'properties' => array(
'name' => $row['LINK_ID']),
'type' => 'Polyline',
'coordinates' => array((float)$row['lon1'],(float)$row['lat1'],(float)$row['lon2'],(float)$row['lat2'],(float)$row['lon3'],(float)$row['lat3'],(float)$row['lon4'],(float)$row['lat4'],(float)$row['lon5'],(float)$row['lat5'],(float)$row['lon6'],(float)$row['lat6'],(float)$row['lon7'],(float)$row['lat7'],(float)$row['lon8'],(float)$row['lat8'],(float)$row['lon9'],(float)$row['lat9'])
)
);
// array_push($geojson, $feature);
array_push($geojson['features'], $feature);
Thank you
Unfortunately, MySQL does not have a native ST_AsGeoJSON function like PostGIS. Fortunately, you can use geoPHP in combination with a little script I wrote to achieve the same result.
Export your GeoJSON file to a MySQL spatial table using OGRor QGIS.
Download geoPHP and my MySQL to GeoJSON script.
Fill in the proper db connection settings and this script should output your MySQL spatial table in proper GeoJSON format.

Categories