sum of array based on key(Group by user) - php

enter image description here
This is my first array which i get from mysql
Array
([0] => Array
(
[created_by] => 4
[count] => 1
[total] => 300
)
[1] => Array
(
[created_by] => 4
[count] => 1
[total] => 450
)
[2] => Array
(
[created_by] => 3
[count] => 1
[total] => 500
)
)
I need a output as
[0] => Array
(
[created_by] => 4
[count] => 2
[total] => 750
)
[1] => Array
(
[created_by] => 3
[count] => 1
[total] => 500
)
Sum of array total based on created_by
Need to group by created_by count.

Related

Joining two multidimensional arrays

I have two arrays each coming from different DB queries, I can't join them in DB so I have to join them using php.
The first:
Array
(
[0] => Array
(
[id] => 23
[host_id] => 5
[pos_id] => 2
[status] => 1
)
[1] => Array
(
[id] => 25
[host_id] => 5
[pos_id] => 1
[status] => 1
)
[2] => Array
(
[id] => 24
[host_id] => 5
[pos_id] => 2
[status] => 1
)
)
And I wanna join it with this array on pos_id and id
Array
(
[0] => Array
(
[id] => 1
[name] => Front
)
[1] => Array
(
[id] => 2
[name] => Back
)
)
so that, the result is:
Array
(
[0] => Array
(
[id] => 23
[host_id] => 5
[pos_id] => 2
[name] => Back
[status] => 1
)
[1] => Array
(
[id] => 25
[host_id] => 5
[pos_id] => 1
[name] => Front
[status] => 1
)
[2] => Array
(
[id] => 24
[host_id] => 5
[pos_id] => 2
[name] => Back
[status] => 1
)
)
The code I have uses an inner loop, matches the value and pushes into array:
foreach($events as &$event) {
foreach($positions as $position) {
if($player[pos_id] == $position[id]){
array_push($event,"name",$position[name]);
}
}
}

Create a new array using another array in PHP

I have array of categories like this and I want to merge the same value like 1 and 0 index value etc in another new array how it is possible.
Array
(
[0] => Array
(
[id] => 29
[parentId] => 0
[serviceName] => Hair
[parentServiceImg] => 7ffb7f5aa2a210ba927e9de64ef17f93.jpeg
[subCategory] => Array
(
[id] => 30
[parentId] => 29
[serviceName] => Blow Out
[subCategory] => Array
(
[id] => 31
[parentId] => 30
[serviceName] => Add Clients Hair Extensions & Style
[subCategory] => Array
(
[id] => 49
[parentId] => 31
[serviceName] => Hair style color
[servicePrice] => 100
)
)
)
)
[1] => Array
(
[id] => 1
[parentId] => 0
[serviceName] => Makeup
[parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg
[subCategory] => Array
(
[id] => 2
[parentId] => 1
[serviceName] => Special Event Makeup
[subCategory] => Array
(
[id] => 3
[parentId] => 2
[serviceName] => Add Airbrush
[servicePrice] => 232
)
)
)
[2] => Array
(
[id] => 1
[parentId] => 0
[serviceName] => Makeup
[parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg
[subCategory] => Array
(
[id] => 6
[parentId] => 1
[serviceName] => Photoshot Makeup
[subCategory] => Array
(
[id] => 7
[parentId] => 6
[serviceName] => Add Airbrush
[servicePrice] => 8
)
)
)
[3] => Array
(
[id] => 1
[parentId] => 0
[serviceName] => Makeup
[parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg
[subCategory] => Array
(
[id] => 2
[parentId] => 1
[serviceName] => Special Event Makeup
[subCategory] => Array
(
[id] => 4
[parentId] => 2
[serviceName] => Add Lashes
[servicePrice] => 6
)
)
)
[4] => Array
(
[id] => 29
[parentId] => 0
[serviceName] => Hair
[parentServiceImg] => 7ffb7f5aa2a210ba927e9de64ef17f93.jpeg
[subCategory] => Array
(
[id] => 2
[parentId] => 1
[serviceName] => Special Event Makeup
[subCategory] => Array
(
[id] => 47
[parentId] => 29
[serviceName] => Wedding Day Mother of the Bride / Mother of the Groom Hair Touch Ups
[servicePrice] => 5
)
)
)
)
I want to 0 index value merge with 4 index value and get on 0 index of new array and 1 to 3 index value merge and get on 1 index in new array using php. I need help for this. thanks.
It should help you:
<?php
$your_array = [0=>['array1'], 1=>['array2'],2=>['array3'], 3=>['array4'], 4=>['array5']];
$new_array = [];
$first = array_shift($your_array);
$last = array_pop($your_array);
$array = array_merge($first, $last);
$new_array[] = $array;
$new_array[] = $your_array;
?>

remove array on duplicate value

I need help. I want to remove array on array list when there is duplicate value.
I have this array
Array
(
[11] => Array
(
[0] => Array
(
[count] => 2
[price] => 1000
[total] => 2000
)
[1] => Array
(
[count] => 0
[price] => 124
[total] => 0
)
[2] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[3] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
[12] => Array
(
[0] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[1] => Array
(
[count] => 1
[price] => 124
[total] => 124
)
[2] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[3] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
)
As you may notice that there are duplication value on price key. But I only want to remove an array if the the count is equal to 0. But if the duplicate price has no value on count. It only remove one duplicate array. My expected output will be like this.
Array
(
[11] => Array
(
[0] => Array
(
[count] => 2
[price] => 1000
[total] => 2000
)
[1] => Array
(
[count] => 0
[price] => 124
[total] => 0
)
[2] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
[12] => Array
(
[0] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[1] => Array
(
[count] => 1
[price] => 124
[total] => 124
)
[2] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
)
Please help thanks.

PHP - how to distribute items into two arrays base on values

I have this array,
Array
(
[0] => Array
(
[id] => 1
[name] => Item 1
[price] => 10
[weight] => 200
)
[1] => Array
(
[id] => 2
[name] => Item 2
[price] => 100
[weight] => 20
)
[2] => Array
(
[id] => 3
[name] => Item 3
[price] => 30
[weight] => 300
)
[3] => Array
(
[id] => 4
[name] => Item 4
[price] => 20
[weight] => 500
)
[4] => Array
(
[id] => 6
[name] => Item 6
[price] => 40
[weight] => 10
)
[5] => Array
(
[id] => 7
[name] => Item 7
[price] => 200
[weight] => 10
)
)
And I want to split it into two, so I get this result,
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => Item 1
[price] => 10
[weight] => 200
)
[1] => Array
(
[id] => 3
[name] => Item 3
[price] => 30
[weight] => 300
)
[2] => Array
(
[id] => 7
[name] => Item 7
[price] => 200
[weight] => 10
)
)
[1] => Array
(
[0] => Array
(
[id] => 4
[name] => Item 4
[price] => 20
[weight] => 500
)
[1] => Array
(
[id] => 6
[name] => Item 6
[price] => 40
[weight] => 10
)
[2] => Array
(
[id] => 2
[name] => Item 2
[price] => 100
[weight] => 20
)
)
)
Any ideas?
The tricky of this is that the total price of each package must not be more than 250. while the total weight of each must be as even as possbile (even thought it is impossible). You can see what I mean in this picture,
name weight price
package 1
Item 1 200 10
Item 3 300 30
Item 7 10 200
(total) 510 240
package 2
Item 4 500 20
Item 6 10 40
Item 2 20 100
(total) 530 160
So is it possible to move around the items between these two packages so that total weights are distributed 'evenly'? It seems that it cannot be achieved without 'moving' them manually. I can't think of any way to achieve the goal by automatic programme. Can you?
Sort the array by weight
usort($array, function($a, $b) {return $a['weight']>$b['weight'];});
We init our result tables, with a price and weight counters
$sum=Array(
Array("weight" => 0, "price" => 0, "res" => Array()),
Array("weight" => 0, "price" => 0, "res" => Array()));
Then we loop on array
while ($obj=array_pop($array)) {
We select the lower result array
$ind=($sum[0]['weight']<$sum[1]['weight']) ? 0 : 1;
except if it has reached the max amount
if ($sum[$ind]['price']+$obj['price']>250) {
$ind=1-$ind;
## object will be skipped if it can't fit in the allowed amount
if ($sum[$ind]['price']+$obj['price']>250) continue;
}
and then we fill it
array_push($sum[$ind]['res'], $obj);
$sum[$ind]['weight']+=$obj['weight'];
$sum[$ind]['price']+=$obj['price'];
}
print_r($sum);
Result as expected:
Array
(
[0] => Array
(
[weight] => 510
[price] => 240
[res] => Array
(
[0] => Array
(
[id] => 3
[name] => Item 3
[price] => 30
[weight] => 300
)
[1] => Array
(
[id] => 1
[name] => Item 1
[price] => 10
[weight] => 200
)
[2] => Array
(
[id] => 7
[name] => Item 7
[price] => 200
[weight] => 10
)
)
)
[1] => Array
(
[weight] => 530
[price] => 160
[res] => Array
(
[0] => Array
(
[id] => 4
[name] => Item 4
[price] => 20
[weight] => 500
)
[1] => Array
(
[id] => 2
[name] => Item 2
[price] => 100
[weight] => 20
)
[2] => Array
(
[id] => 6
[name] => Item 6
[price] => 40
[weight] => 10
)
)
)
)
It have nothing to do with PHP or arrays. As I understand your question it's kind of Multiple knapsack problem. So it's lots of math (see some solution here) and maybe easiest way is to programmatically chaeck all possible variants.

Convert flat PHP array to multidimensional array

I have the following array structure:
[parents] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[created] => 2014-11-09 13:47:37
[content] => This is a test discussion
[status] => 1
[parent] => 0
[project_id] => 1
)
[1] => Array
(
[id] => 4
[user_id] => 1
[created] => 2014-11-09 13:52:02
[content] => 456789
[status] => 1
[parent] => 0
[project_id] => 1
)
)
[children] => Array
(
[0] => Array
(
[id] => 2
[user_id] => 1
[created] => 2014-11-09 13:47:53
[content] => This is a test reply....
[status] => 1
[parent] => 1
[project_id] => 1
)
[1] => Array
(
[id] => 3
[user_id] => 1
[created] => 2014-11-09 13:48:13
[content] => This is a test reply....!!!
[status] => 1
[parent] => 1
[project_id] => 1
)
[2] => Array
(
[id] => 5
[user_id] => 1
[created] => 2014-11-09 13:52:17
[content] => 8765432
[status] => 1
[parent] => 4
[project_id] => 1
)
)
I would like to merge them into a parent/child relationship so it looks like follows:
[parents] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[created] => 2014-11-09 13:47:37
[content] => This is a test discussion
[status] => 1
[parent] => 0
[project_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 2
[user_id] => 1
[created] => 2014-11-09 13:47:53
[content] => This is a test reply....
[status] => 1
[parent] => 1
[project_id] => 1
)
[1] => Array
(
[id] => 3
[user_id] => 1
[created] => 2014-11-09 13:48:13
[content] => This is a test reply....!!!
[status] => 1
[parent] => 1
[project_id] => 1
)
)
)
)
How could I go about doing that with PHP?
Assuming that all ids are unique in your parents array, you can do this:
// build a new array using parent's ID values as the key,
// to simplify searching for parent IDs.
foreach ($parents as $key => $value){
$merged[$value['id']] = $value;
}
foreach ($children as $child){
$parentID = $child['parent'];
if (array_key_exists( $parentID, $merged )) {
// add the child array to its parent's ['children'] value
$merged[$parentID]['children'][] = $child;
}
}
In the resulting array $merged, the key of each parent item will be set to its id, and all children will be nested under their corresponding parents.

Categories