I have an array that has the variable name $array and it is as follows:
$array = [
"data"=> [
[
"company"=>[
"id"=> 1,
"name"=> "company1"
],
"reports"=> [
"active_reports"=> 3,
"completed_reports"=> 2
]
],
[
"company"=>[
"id"=> 2,
"name"=> "company2"
],
"reports"=> [
"active_reports"=> 6,
"completed_reports"=> 1
]
],
[
"company"=>[
"id"=> 2,
"name"=> "company2"
],
"reports"=> [
"active_reports"=> 7,
"completed_reports"=> 5
]
]
]
];
So, I want to group this array by the company id. The id can be found in the nested array with key company
My expected result is below:
{
"1": [
{
"company": {
"id": 1,
"name": "company1"
},
"reports": {
"active_reports": 3,
"completed_reports": 2
}
}
],
"2": [
{
"company": {
"id": 2,
"name": "company2"
},
"reports": {
"active_reports": 6,
"completed_reports": 1
}
},
{
"company": {
"id": 2,
"name": "company2"
},
"reports": {
"active_reports": 7,
"completed_reports": 5
}
}
]
}
So, I tried the following logic:
foreach ($array['data'] as $data) {
$reportsData = collect($data)->groupBy($data['company']['id']);
Log::info($reportsData);
}
But this is the result I'm getting after trying the above logic:
[
{
"": [
{
"id": 1,
"name": "company1"
},
{
"active_reports": 3,
"completed_reports": 2
}
]
},
{
"": [
{
"id": 2,
"name": "company2"
},
{
"active_reports": 6,
"completed_reports": 1
}
]
},
{
"": [
{
"id": 2,
"name": "company2"
},
{
"active_reports": 7,
"completed_reports": 5
}
]
}
]
I want to be able to get the expected result as illustrated above.
You are passing a value to groupBy currently not a 'column' to group by. Though you have nested data here so you will need to pass a callback to groupBy where you will return what value the entities should be grouped by:
collect($array['data'])
->groupBy(fn ($item) => $item['company']['id']);
Or simply using the 'dot' notation (as mentioned by Donkarnash), since we are starting the collection at the 'data' key:
collect($array['data'])->groupBy('company.id');
Laravel 9.x Docs - Collections - Available Methods - groupBy
Related
I am working with php in laravel, in this case I have 2 collections of objects, one of them looks like this:
[
{
"id": 1,
"name": "product x",
"quantity": "100",
},
{
"id": 2,
"codProd": "product y",
"quantity": "200",
},
{
"id": 3,
"name": "product a.",
"quantity": "30",
}
]
and the other looks like this:
[
{
"reference": 1,
"quantity": "80",
},
{
"reference": 2,
"quantity": "50",
},
]
What I need is to keep the first collection but adding the value of the quantity key from the second collection, using the reference key as a relationship with the id of the first collection, the final result should look like this:
[
{
"id": 1,
"name": "product x",
"quantity": "180",
},
{
"id": 2,
"codProd": "product y",
"quantity": "250",
},
{
"id": 3,
"name": "product a.",
"quantity": "30",
}
]
so how can I do this?, any guide or help I am grateful
You can use a collection method for this, the map method:
$collection_one = collect([
[
'id' => 1,
'name' => 'product x',
'quantity' => 100,
],
[
'id' => 2,
'name' => 'product y',
'quantity' => 100,
],
[
'id' => 1,
'name' => 'product a.',
'quantity' => 30,
],
]);
$collection_two = collect([
[
'reference' => 1,
'quantity' => 80,
],
[
'reference' => 2,
'quantity' => 50,
],
]);
For you to get the collection what you want:
$collect = [];
$collect = $collection_one->map(function ($value_one) use ($collection_two) {
if ($search = $collection_two->firstWhere('reference', '=', $value_one['id'])) {
$value_one['quantity'] += $search['quantity'];
}
return $value_one;
});
Ready, that worked for me. :)
I am building a Laravel app that connected with MongoDB. Now I am facing an issue when query data from the database.
table name: Employee
[
{
"_id": 1,
"name": "user 1",
"managers": [
{
"id": 321,
"user_id": 1,
"ack_groups: [
["2","3", "4"],
["2", "5", "6"]
]
},
......
]
},
{
"_id": 2,
"name": "user 2",
"managers": [
{
"id": 3213,
"user_id": 2,
"ack_groups: [
["6","7", "8"],
["2", "5", "6"]
]
},
......
]
},
{
"_id": 3,
"name": "user 3",
"managers": [
{
"id": 321,
"user_id": 3,
"ack_groups: [
["8","9", "14"],
["12", "15", "16"]
]
},
......
]
},
]
I am trying to query list of employees data from Employee table with some conditions. The condition is that I want to get list of employees where ac_groups in managers equal to 2.
Then the output should be:
[
{
"_id": 1,
"name": "user 1",
"managers": [
{
"id": 321,
"user_id": 1,
"ack_groups: [
["2","3", "4"],
["2", "5", "6"]
]
},
......
]
},
{
"_id": 2,
"name": "user 2",
"managers": [
{
"id": 3213,
"user_id": 2,
"ack_groups: [
["6","7", "8"],
["2", "5", "6"]
]
},
......
]
},
]
What I have tried in code: Employee::where('verify_routes.managers.ack_groups', '=', "2")->get();
Forum: "Find in array value"
Doc: Specific Operators
This should work:
Employee::where('verify_routes.managers.ack_groups', 'all', [2])->get();
I have this payload,
[
{
"id": 1,
"name": "T-Shirt",
"children_rec": [
{
"id": 2,
"name": "Classic",
"children_rec": [
{
"id": 3,
"name": "Lycra",
"children_rec": []
}
]
},
{
"id": 4,
"name": "Plain",
"children_rec": []
}
]
},
{
"id": 5,
"name": "Shirt",
"children_rec": [
{
"id": 6,
"name": "Plain",
"children_rec": []
}
]
}
]
I want to loop through every occurrence of children_rec.
What I have tried is,
foreach ($mainCategories as $category) {
if (!empty($category['children_rec'])) {
foreach ($category['children_rec'] as $child) {
if (!empty($category['children_rec'])) {
var_dump($child);
}
}
}
}
But this is not the dynamic way to achieve this. What if I have 5 or 6 level of childer_rec. How can I achieve this?
Edit
#ggorlen's way is cool, but What if I need this output?
[
{
"id": 1,
"name": "T-Shirt",
"children": [
"2",
"3",
"4"
]
},
{
"id": 5,
"name": "Shirt",
"children": [
"6"
]
}
]
Since you don't know the depth, you'll need a stack or recursion. Here's a solution with a stack:
<?php
$tree = json_decode('[ { "id": 1, "name": "T-Shirt", "children_rec": [ { "id": 2, "name": "Classic", "children_rec": [ { "id": 3, "name": "Lycra", "children_rec": [] } ] }, { "id": 4, "name": "Plain", "children_rec": [] } ] }, { "id": 5, "name": "Shirt", "children_rec": [ { "id": 6, "name": "Plain", "children_rec": [] } ] } ]', true);
for ($stack = $tree; !empty($stack);) {
$curr = array_pop($stack);
$flattened[] = $curr["name"]; // or just $curr if you only want the node
if (!empty($curr["children_rec"])) {
array_push($stack, ...$curr["children_rec"]);
}
}
print_r($flattened);
Array
(
[0] => Shirt
[1] => Plain
[2] => T-Shirt
[3] => Plain
[4] => Classic
[5] => Lycra
)
If order is important, you can array_reverse all arrays as you push them onto the stack without harming the linear time complexity.
For the updated specification, just plop the above code into a function and call it for each root node:
<?php
function flatten($tree) {
for ($stack = array_reverse($tree); !empty($stack);) {
$curr = array_pop($stack);
$flattened[] = $curr["id"];
if (!empty($curr["children_rec"])) {
array_push($stack, ...array_reverse($curr["children_rec"]));
}
}
return $flattened;
}
$tree = json_decode('[ { "id": 1, "name": "T-Shirt", "children_rec": [ { "id": 2, "name": "Classic", "children_rec": [ { "id": 3, "name": "Lycra", "children_rec": [] } ] }, { "id": 4, "name": "Plain", "children_rec": [] } ] }, { "id": 5, "name": "Shirt", "children_rec": [ { "id": 6, "name": "Plain", "children_rec": [] } ] } ]', true);
foreach ($tree as &$root) {
$root["children"] = flatten($root["children_rec"]);
unset($root["children_rec"]);
}
echo json_encode($tree, JSON_PRETTY_PRINT)."\n";
Output:
[
{
"id": 1,
"name": "T-Shirt",
"children": [
2,
3,
4
]
},
{
"id": 5,
"name": "Shirt",
"children": [
6
]
}
]
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"
}
}
]
I have an associative array object :
[ {
"id": 15,
"owner_id": 1,
"container_info": {
"id": 1,
"container_id": 15
},
"filters": [
{
"id": 3,
"parent_id": null
},
{
"id": 6,
"parent_id": null
}
],
"children_recursive": [
{
"id": 7,
"owner_id": 1,
"container_info": null,
"filters": [
],
"children_recursive": [
{
"id": 8,
"owner_id": 1,
"container_info": null,
"filters": [
],
"children_recursive": [
]
}
]
},
{
"id": 16,
"owner_id": 1,
"container_info": {
"id": 2,
"container_id": 16
},
"filters": [
],
"children_recursive": [
]
},
]
}
]
I want to recursively loop through all object and their children_recursive key. And each children_recursive object (at any depth) needs to be processed
So I used :
public function traverseContainerRecursively($containerItems)
{
Log:info(' CHECK 1');
foreach ($containerItems as $containerItem) {
Log::info(json_encode($containerItem->id));
Log::info(json_encode($containerItem->owner_id));
Log::info(json_encode($containerItem->container_info));
Log::info(json_encode($containerItem->children_recursive));
}
Log::info(' CHECK 2');
foreach ($containerItems as $containerItem) {
Log::info(json_encode($containerItem['id']));
Log::info(json_encode($containerItem['owner_id']));
Log::info(json_encode($containerItem['container_info']));
Log::info(json_encode($containerItem['children_recursive']));
}
Log::info(' CHECK 3');
foreach ($containerItems as $key=>$value) {
if( $key == "children_recursive" ) {
Log::info(json_encode($value));
$this->traverseContainerRecursively($value);
}
} //foreach end
}
OUTPUT :
CHECK 1
15
1
null
null
CHECK 2
15
1
null
null
CHECK 3
{
"id": 15,
"owner_id": 1,
"container_info": {
"id": 1,
"container_id": 15
},
"filters": [
{
"id": 3,
"parent_id": null
},
{
"id": 6,
"parent_id": null
}
],
"children_recursive": [
{
"id": 7,
"owner_id": 1,
"container_info": null,
"filters": [
],
"children_recursive": [
{
"id": 8,
"owner_id": 1,
"container_info": null,
"filters": [
],
"children_recursive": [
]
}
]
},
{
"id": 16,
"owner_id": 1,
"container_info": {
"id": 2,
"container_id": 16
},
"filters": [
],
"children_recursive": [
]
},
]
} // i.e. the entire passed object
So I am unable to retrieve the value for key "children_recursive".
Please guide.
I figured out the issue.
Following code worked:
$obj = json_decode($containerItem);
Log::info(json_encode($obj->children_recursive));