Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 months ago.
Improve this question
I have array, like this and I want to join him by name:
Please help to decide problem.
$params[] =
[
"name" => "Robert"
"visitCount" => 2
"coef" => "5.50"
],
[
"name" => "Anna"
"visitCount" => 1
"coef" => "3.58"
],
[
"name" => "Joe"
"visitCount" => 1
"coef" => "8.00"
],
[
"name" => "Robert"
"visitCount" => 2
"coef" => "1.50"
]
How I can get concatenation similar name and sum of coef?
Result that I need:
[
"name" => "Robert"
"visitCount" => 2
"coef" => "7.00"
],
[
"name" => "Anna"
"visitCount" => 1
"coef" => "3.58"
],
[
"name" => "Joe"
"visitCount" => 1
"coef" => "8.00"
],
$output = [];
foreach ($params as $obj) {
$output[$obj['name']] = [
'name' => $obj['name'],
'visitCount' => $obj['visitCount'],
'coef' => (isset($output[$obj['name']])) ? $obj['coef'] + $output[$obj['name']]['coef'] : $obj['coef']
];
}
// If you not want an assossiative array
$output = array_values($output);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 days ago.
Improve this question
I am having array of php with parent and children mapping on mentioned columns, I had wasted more time doing r&d and execution on this can anyone please suggest on this.
$arr = [
0 => [
"id" = 1,
"parent_id" => 0,
],
1 => [
"id" = 2,
"parent_id" => 1,
],
2 => [
"id" = 3,
"parent_id" => [1,2],
],
3 => [
"id" = 4,
"parent_id" => 1,
],
4 => [
"id" = 5,
"parent_id" => 4,
],
];
i am expecting output
$output = [
0 => [1,2,3],
1=> [1,4,5]
]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 days ago.
Improve this question
I have one multi dimensional array in php. I want Sum of multidimensional array in php based on key value
$array = [
0 => [
"id" => "1",
"title" => "Title 1",
"price" => "2.50",
],
1 => [
"id" => "2",
"title" => "Title 2",
"price" => "2.50",
],
2 => [
"id" => "1",
"title" => "Title 1",
"price" => "2.50",
]
];
I want below output as merge array and sum of price.
OUTPUT
$array = [
0 => [
"id" => "1",
"title" => "Title 1",
"price" => "5.0",
],
1 => [
"id" => "2",
"title" => "Title 2",
"price" => "2.50",
]
];
Please help me guys.
I have tried with arrray_search and array_key_exist. but can't find any solution.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
I am new to PHP and I have dabbled with basic arrays from the video tutorials. I need some help in converting a PHP multidimensional array into a HTML table. I have already wasted a bunch of time in trying to get in the format I wanted, and thought someone in this forum will have an easy and better answer for me. Thank you before hand.
Here is the PHP array format -
[ ["term" => 3, "val" => 0.7, "user" => ["name" => "user1"], "dept" => ["dep" => "dept1"]],
["term" => 3, "val" => 0.6, "user" => ["name" => "user1"], "dept" => ["dep" => "dept1"]],
["term" => 12, "val" => 0.5, "user" => ["name" => "user1"], "dept" => ["dep" => "dept1"]],
["term" => 3, "val" => 0.1, "user" => ["name" => "user2"], "dept" => ["dep" => "dept1"]],
["term" => 6, "val" => 0.2, "user" => ["name" => "user2"], "dept" => ["dep" => "dept1"]],
["term" => 9, "val" => 0.3, "user" => ["name" => "user2"], "dept" => ["dep" => "dept1"]],
["term" => 3, "val" => 0.4, "user" => ["name" => "user3"], "dept" => ["dep" => "dept1"]],
["term" => 6, "val" => 0.5, "user" => ["name" => "user3"], "dept" => ["dep" => "dept1"]],
["term" => 12, "val" => 0.6, "user" => ["name" => "user3"], "dept" => ["dep" => "dept1"]]
]
and here is the out put format I am looking for -
Basically, the table should list all the users along the rows and the columns should be the unique values of the 'terms' in the array. the values in the table should be field 'val'. In case there are multiple values for 'val', then it should be the maximum value of the 'val'.
I hope I was clear in my q. I look forward for your help.
I would suggest transforming your array first so that it is grouped by the user so that you can loop more sensibly. Essentially you would end up with:
[
[
"user" => [
"name" => "user1"
],
"terms" => [
"3" => 1,
"6" => 0,
"9" => 0,
"12" => 0
]
],
[
"user" => [
"name" => "user2"
]
"terms" => [ ... ]
]
]
From there, it's easy to loop through each user and output the relevant data
...
# in your HTML file
<?php foreach($users_data as $user_data){ ?>
<tr>
<td><?php echo $user_data["user"]["name"]; ?></td>
<td><?php echo $user_data["terms"]["3"]; ?></td>
<td><?php echo $user_data["terms"]["6"]; ?></td>
<td><?php echo $user_data["terms"]["9"]; ?></td>
<td><?php echo $user_data["terms"]["12"]; ?></td>
</tr>
<?php } ?>
...
In my program i have arrays in the following format . I need to find whether a value is present in an the array
return [
"affiliates" => [
"name" => 'Affiliates',
"value" => 11
],
"business" => [
"name" => 'Business',
"value" => 12
],
"inquiries" => [
"name" => 'Inquiries',
"value" => 13
],
"students" => [
"name" => 'Students',
"value" => 14
],
"teachers" => [
"name" => 'Teachers',
"value" => 15
],
"Personal" => [
"name" => 'Personal',
"value" => 3
],
];
I am doing the following. I am expecting that the search will find the value(12) which is a business, but It is returning false.
$searchcategoryid = '12';
$key = array_search($searchcategoryid, array_column(config('peopletypes.students'), 'value'));
Please note that config('peopletypes.students')will return the array above mentioned. I am using laravel.
You have some sort of configuration problem, this code prints 1.
<?php
$searchcategoryid = '12';
$key = array_search($searchcategoryid, array_column([
"affiliates" => [
"name" => 'Affiliates',
"value" => 11
],
"business" => [
"name" => 'Business',
"value" => 12
],
"inquiries" => [
"name" => 'Inquiries',
"value" => 13
],
"students" => [
"name" => 'Students',
"value" => 14
],
"teachers" => [
"name" => 'Teachers',
"value" => 15
],
"Personal" => [
"name" => 'Personal',
"value" => 3
],
], 'value'));
echo $key;
Try to make sure that your config function actually returns the array you claim it to.
This question already has answers here:
Nested query in elasticsearch
(1 answer)
ElasticSearch - Malformed Query (RoR)
(1 answer)
Combine must and should
(1 answer)
bool malformed query, expected END_OBJECT but found FIELD_NAME
(1 answer)
Closed 4 years ago.
I've been messing around with this for a while but can't get it right. I've tried several different ways of displaying this query. For example, I've made each param inside of bool a key instead of an array element. As it is now it's an array element. Either way ES doesn't seem to like it. What would be the proper format for the following query?
What it's supposed to do:
This is PHP. The should query is either in between two dates and must have the status ended OR it must start at one date and the status cannot be ended.
[
'index' => 'quotes',
'type' => 'title',
'body' => [
'query' => [
"function_score" => [
'query' => [
'bool' => [
'filter' => [
["range" => ["number_of_quotes" => ["gt" => 0]]],
["exists" => ["field" => "vote-average"]],
["exists" =>["field" => "first-air-date"]],
["exists" =>["field" => "last-air-date"]],
],
'should' => [
['bool' => [
["range" => ["first-air-date" => [
"gte" => "$begin",
"format" => "yyyy"
],
["last-air-date" => [
"lte" => $begin,
"format" => "yyyy"
]],
]],
["must" => [
"match" => [
"status" => "Ended"
]
]
]],
],
['bool' => [
["range" => ["first-air-date" => [
"gte" => "$begin",
"format" => "yyyy"
]]],
["must_not" => [
"match" => [
"status" => "Ended"
]
]
]],
]]],
]
],
],
],
"from" => (int) $skip,
"size" => (int) $take,
];