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]
]
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 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 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);
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 } ?>
...
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,
];
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 years ago.
Improve this question
Algorithm question which I seem to be having trouble wrapping my head around today if someone can point me in the right direction. It feels like something which should already be solved that I could lookup but I am having difficulty finding it.
I have some ranges and I want to get new unique non-overlapping ranges from these. For example:
My ranges
[
[0] => [
'Min' => 5,
'Max' => 10
],
[1] => [
'Min' => 5,
'Max' => 12
],
[2] => [
'Min' => 13,
'Max' => 15
],
[3] => [
'Min' => 13,
'Max' => 18
],
[4] => [
'Min' => 20,
'Max' => 24
],
]
What I would like out is:
Note keys 2 and 4 have changed different fixing the overlapping issue.
[
[0] => [
'Min' => 5,
'Max' => 10
],
[1] => [
'Min' => 11,
'Max' => 12
],
[2] => [
'Min' => 13,
'Max' => 15
],
[3] => [
'Min' => 16,
'Max' => 18
],
[4] => [
'Min' => 20,
'Max' => 24
],
]
Note how 19 is not there as its not a number within any of the original ranges.
I've tried searching but can't seem to think of what this kind of algorithm would be named so I am getting lots of irrelevant results.
Also notes how non of the ranges at the end overlap and contain the numbers unique (either in a min or max).
Thanks,
This is how I would go about:
Order the array entries with usort. First by Min ascending, then by Max descending.
Initialize $upperLimit with 0.
Then, loop over the array and
Set Min to max($upperLimit + 1, ['Min'])
If Min is >= Max, throw away this entry
Set $upperLimit to max($upperLimit, ['Max'])