How can I get just the value of the information? - php

I want to get only the value of the kilometres to save it in a new variable.
I also tried to access the array with index.
My code:
/**
* #Route("/routesetting", name="api_routeset_set", methods={"GET"})
*/
public function getRouteAction(Request $request) {
$distanceMatrix = new GoogleDistanceMatrix('AIzaSyBdAHFe4MsbIr417NlPioNhYW7as-adBa8');
$distance = $distanceMatrix
->addOrigin('Van Bronckhorststraat 94, 5961SM Horst, The Netherlands')
->addDestination('Maistraße 10, 80337 München, Deutschland')
->setMode(GoogleDistanceMatrix::MODE_DRIVING)
->setLanguage('en-EN')
->setUnits(GoogleDistanceMatrix::UNITS_METRIC)
->setAvoid(GoogleDistanceMatrix::AVOID_FERRIES)
->sendRequest();
return $this->json([
'message' => $distance
]);
}
Result:
{
"message": {
"status": "OK",
"responseObject": [],
"originAddresses": [
[]
],
"destinationAddresses": [
[]
],
"rows": [
{
"elements": [
{
"status": "OK",
"duration": {
"text": "6 hours 48 mins",
"value": 24461
},
"distance": {
"text": "695 km",
"value": 695380
}
}
]
}
]
}
}
Expected result:
695380

$distance here seems to be a PHP array (I've never worked with this Google API).
So just before calling return $this->json(['message' => $distance]); you should just get the value you want from the $distance variable.
Which seems to be $distance['rows']['elements']['distance']['value'].
So returning $this->json(['message' => $distance['rows']['elements']['distance']['value']]); seems to be your trick. ;)
Be careful I haven't done any content verification here (isset()).

Related

validated multiple nested array items in laravel

i have a nested array to validate which i looked at other questions and valited that . but my problem begins when i multiple the array like below :
{
"items": [
{
"sender": {
"firstName": "firstName",
"lastName": "lastName",
},
"items": [
{
"weight": {
"value": 1000
}
}
]
},
{
"sender": {
"firstName": "firstName",
"lastName": "lastName",
},
"items": [
{
"weight": {
"value": 1000
}
}
]
}
]
}
now what i want to do is to validate senders and value to exists and check types of them .
what i have tried so far is :
first i send the $data = $request->get('items'); to the validator and then .
public function rules()
{
return [
"sender.*.firstName" => "required|string|max:17",
but i all the time get this error on validation :
{
"detail": "",
"message": {
"sender.lastName.firstName": [
"sender.lastName.firstName is required."
],
can you please give some advice how should i validate that array ?? thanks
it's items.*.sender.firstName. You have to put the * where the array is
You should not remove items from the data.
You can try *.sender.firstName but i'm not sure if it will work, i think it needs a toplevel field

php echo from a start point to a end point

I have built a php site that takes code from a page using GET, and then echos it to the current page, but it exports text like this,
{ "selection13": [ { "name": "L" }, { "name": "100%\nA" } ], "selection7": [ { "name": "S" }, { "name": "100%\nA" } ], "selection8": [ { "name": "SS" }, { "name": "100%\nA" } ], "selection9": [ { "name": "SP" }, { "name": "100%\nA" } ], "selection10": [ { "name": "P" }, { "name": "100%\nA" } ], "selection11": [ { "name": "A" }, { "name": "100.00%\nA+" } ], "selection12": [ { "name": "H", "selection5": [ { "name": "T }, { "name": "100.00%\nA+" } ] }, { "name": "100.00%\nA+", "selection5": [ { "name": "T" }, { "name": "100.00%\nA+" } ] } ] }
I need to organizes it in to categorizes like, {L},{100%), here is the code I am currently using,
<?php
$params = http_build_query(array(
"api_key" => "()",
"format" => "json"
));
$result = file_get_contents(
'(url)'.$params,
false,
stream_context_create(array(
'http' => array(
'method' => 'GET'
)
))
);
echo gzdecode($result);
?>
Your script is outputting JSON but there appears to be a few issues with the data itself that need to be addressed first.
The next to the last "name": "T is missing a closing quote.
Section 12 has an inconsistent organization compared to the other sections. Can a section be a child of another section or is this an error?
"selection11": [
{
"name": "A"
},
{
"name": "100.00%\nA+"
}
],
"selection12": [
{
"name": "H",
"selection5": [
{
"name": "T"
},
{
"name": "100.00%\nA+"
}
]
},
{
"name": "100.00%\nA+",
"selection5": [
{
"name": "T"
},
{
"name": "100.00%\nA+"
}
]
}
]
These 2 issues need to be fixed in the JSON data you are retrieving first.
From there, the best way to reorganize this is to convert it to an array using json_decode.
$json = gzdecode($result);
$resultArray = json_decode($json);
I can't tell from your question exactly how you want to reorganize the data but use one or more of the builtin array functions to get the data in the structure you want. If you need to output it in JSON, use json_encode on the manipulated array to get the final data format. More specific help can be provided if you can be more clear on want output structure you are looking for and the format (does it need to be JSON or something else).

laravel change object when use groupBy in collection

when i use groupBy with laravel
i use it for api but response show like this example
$data = Order::where('id_user',Auth::id())->with('resturant')->orderBy('created_at', 'desc')->get()->groupBy('uuid');
return response()->json([
'data'=>$dataa,
'state'=>true,
]);
response showing
"data": {
"11447": [
{
"id": 75704,
"uuid": "11447",
...........
"resturant": {
"id": 5,
"category_id": 1,
...........
}
}
],
"49262": [
{
"id": 75702,
"uuid": "49262",
...........
"resturant": {
"id": 5,
"category_id": 1,
...........
}
},
]
},
"state": true
}
i need remove number in object
"11447": [
how ?!
please use this
$data = Order::select(DB::raw('id,uuid,count(*) as count_order'))->groupBy('uuid')->get();
return response()->json([
'data'=>$data,
'state'=>true,
]);

PHP and JSON. How to get a specific element? //Multidimensional Array

My JSON file
{
"shopId": 29,
"last": 46977914,
"freshfood": [
{
"freshfood_id": 2629,
"food": [
{
"food_id": 1740851,
"type": "fruit",
"status": 1
},
{
"food_id": 1730905,
"type": "vegetable",
"status": 1
},
]
}
]
}
I need to get second food_id (1730905)
I try this, but it does not work.
$string = file_get_contents("food.json");
$json_a=json_decode($string,true);
echo $GetFreshFoodId = $json_a['freshfood'][1]['freshfood_id'];
$json_a['freshfood']['food'][1]['food_id'];
It´s a syntrax error in your json file.
Your last entry in "food": [ ... ] has a comma.
That´s the reason why you get NULL when you´re run json_decode
{
"shopId": 29,
"last": 46977914,
"freshfood": [
{
"freshfood_id": 2629,
"food": [
{
"food_id": 1740851,
"type": "fruit",
"status": 1
},
{
"food_id": 1730905,
"type": "vegetable",
"status": 1
},
]
}
]
}

ElasticSearch match combination in array

I'm implementing ElasticSearch into my Laravel application using the php package from ElasticSearch.
My application is a small jobboard and currently my job document is looking like this:
{
"_index":"jobs",
"_type":"job",
"_id":"19",
"_score":1,
"_source":{
"0":"",
"name":"Programmer",
"description":"This is my first job! :)",
"text":"Programming is awesome",
"networks":[
{
"id":1,
"status":"PRODUCTION",
"start":"2015-02-26",
"end":"2015-02-26"
},
{
"id":2,
"status":"PAUSE",
"start":"2015-02-26",
"end":"2015-02-26"
}
]
}
}
As you can see a job can be attached to multiple networks. In my search query I would like to include WHERE network.id == 1 AND network.status == PRODUCTION.
My current query looks like this, however this returns documents where it has a network of id 1, if it has any network of status PRODUCTION. Is there anyway i can enforce both to be true within one network?
$query = [
'index' => $this->index,
'type' => $this->type,
'body' => [
'query' => [
'bool' => [
'must' => [
['networks.id' => 1]],
['networks.status' => 'PRODUCTION']]
],
'should' => [
['match' => ['name' => $query]],
['match' => ['text' => $query]],
['match' => ['description' => $query]],
],
],
],
],
];
You need to specify that the objects in the networks array should be stored as individual objects in the index, this will allow you to perform a search on individual network objects. You can do so using the nested type in Elasticsearch.
Also, if you doing exact matches it is better to use a filter rather than a query as the filters are cached and always give you better performance than a query.
Create your index with a new mapping. Use the nested type for the networks array.
POST /test
{
"mappings": {
"job": {
"properties": {
"networks": {
"type": "nested",
"properties": {
"status": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}
}
Add a document:
POST /test/job/1
{
"0": "",
"name": "Programmer",
"description": "This is my first job! :)",
"text": "Programming is awesome",
"networks": [
{
"id": 1,
"status": "PRODUCTION",
"start": "2015-02-26",
"end": "2015-02-26"
},
{
"id": 2,
"status": "PAUSE",
"start": "2015-02-26",
"end": "2015-02-26"
}
]
}
As you have a nested type you will need to use a nested filter.
POST /test/job/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "networks",
"filter": {
"bool": {
"must": [
{
"term": {
"networks.id": "1"
}
},
{
"term": {
"networks.status.raw": "PRODUCTION"
}
}
]
}
}
}
}
}
}
}

Categories