I have an array of arrays that I need to consolidate into another array.
I have tried mapping over it, matching object_id, and gathering all account_ids for said object_id, but all my attempts are not even close as I am trying to learn PHP
This is the original array
[0] => Array
(
[rank] => 0
[id] => 6
[object_id] => 3
[account_id] => 13
)
[1] => Array
(
[rank] => 1
[id] => 7
[object_id] => 3
[account_id] => 565
)
[2] => Array
(
[rank] => 2
[id] => 1823
[object_id] => 825
[account_id] => 563
)
[3] => Array
(
[rank] => 3
[id] => 1824
[object_id] => 825
[account_id] => 564
)
[4] => Array
(
[rank] => 4
[id] => 1825
[object_id] => 825
[account_id] => 565
)
[5] => Array
(
[rank] => 5
[id] => 7187
[object_id] => 3113
[account_id] => 564
)
[6] => Array
(
[rank] => 6
[id] => 7188
[object_id] => 3113
[account_id] => 565
)
This is the desired result
[3] => [13, 565],
[825] => [563, 564, 565],
[3113] => [564, 565],
You need to create a new array by using object_id index.
Example:
<?
$array = array(
array('rank'=>0,'id'=>6,'object_id'=>3,'account_id'=>13),
array('rank'=>1,'id'=>7,'object_id'=>3,'account_id'=>565),
array('rank'=>2,'id'=>1823,'object_id'=>825,'account_id'=>563),
array('rank'=>3,'id'=>1824,'object_id'=>825,'account_id'=>564),
array('rank'=>4,'id'=>1825,'object_id'=>825,'account_id'=>565),
array('rank'=>5,'id'=>7187,'object_id'=>3113,'account_id'=>564),
array('rank'=>6,'id'=>7188,'object_id'=>3113,'account_id'=>565),
);
$newArray = array(); // initiliaze array
foreach ($array as $key => $value) {
$newArray[$value['object_id']][] = $value['account_id']; // save it in group
}
echo "<pre>";
print_r($newArray); // result
?>
Running Example
I want key rating sum common slug key value. Please help me out.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 6
[slug] => scenario
[avis_id] => 2
[rating] => 0
)
[1] => Array
(
[id] => 7
[slug] => jeu_d_acteur
[avis_id] => 2
[rating] => 9
)
[2] => Array
(
[id] => 8
[slug] => effets_speciaux
[avis_id] => 2
[rating] => 0
)
[3] => Array
(
[id] => 9
[slug] => prises_de_vues
[avis_id] => 2
[rating] => 0
)
[4] => Array
(
[id] => 10
[slug] => bande_son
[avis_id] => 2
[rating] => 6.8
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[slug] => scenario
[avis_id] => 1
[rating] => 8
)
[1] => Array
(
[id] => 2
[slug] => jeu_d_acteur
[avis_id] => 1
[rating] => 5
)
[2] => Array
(
[id] => 3
[slug] => effets_speciaux
[avis_id] => 1
[rating] => 5
)
[3] => Array
(
[id] => 4
[slug] => prises_de_vues
[avis_id] => 1
[rating] => 6
)
[4] => Array
(
[id] => 5
[slug] => bande_son
[avis_id] => 1
[rating] => 8
)
)
)
output:
Array
(
[slug] => scenario
[rating] => 8
)
[1] => Array
(
[slug] => jeu_d_acteur
[rating] => 14
)
[2] => Array
(
[slug] => effets_speciaux
[rating] => 3
)
[3] => Array
(
[slug] => prises_de_vues
[rating] => 6
)
[4] => Array
(
[slug] => bande_son
[rating] => 14.8
)
)
You can merge them by using ... splat operator and loop through to group by slug and sum rating
$merged = array_merge(...$a);
$r = [];
foreach($merged as $v){
isset($r[$v['slug']])
? ($r[$v['slug']]['rating'] += $v['rating'] )
: ($r[$v['slug']] = ['slug'=>$v['slug'],'rating'=>$v['rating']]);
}
Working example :- https://3v4l.org/odCl6
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]);
}
}
}
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.
I am trying to merge 2 arrays a single array on a multidimensional array where a given key-value = a value
the first array looks like this:
Array
(
[0] => Array
(
[id] => 4
[subcategories] => Array
(
[0] => Array
(
[id] => 5
[category_order] => 0
[parent_id] => 4
[name] => Audio Equipment
)
[1] => Array
(
[id] => 6
[category_order] => 0
[parent_id] => 4
[name] => Home Entertainment
)
[2] => Array
(
[id] => 7
[category_order] => 0
[parent_id] => 4
[name] => Photography
)
[3] => Array
(
[id] => 8
[category_order] => 0
[parent_id] => 4
[name] => Portable Audio
)
[4] => Array
(
[id] => 9
[category_order] => 0
[parent_id] => 4
[name] => Televisions
)
)
)
)
and the second like this:
Array
(
[0] => Array
(
[id] => 10
[parent_id] => 5
[name] => Amplifiers & Receivers
)
[1] => Array
(
[id] => 11
[parent_id] => 5
[name] => Audio Systems
)
[2] => Array
(
[id] => 12
[parent_id] => 5
[name] => Cassette Decks
)
[3] => Array
(
[id] => 13
[parent_id] => 5
[name] => CD Players
)
[4] => Array
(
[id] => 14
[parent_id] => 5
[name] => Radios
)
[5] => Array
(
[id] => 15
[parent_id] => 5
[name] => HiFi Speakers
)
)
What I want to do is add each of the second arrays to a sub array of the first multidimensional array where the parent_id of the second array = the id of the subcategories array of the first array so it will look like this:
array
(
[0]=> Array
(
[id] => 4
[subcategories] => Array
(
[0] => Array
(
[id] => 5
[category_order] => 0
[parent_id] => 4
[name] => Audio Equipment
[subsubcategories] = array
(
[id] => 10
[parent_id] => 5
[name] => Amplifiers & Receivers
)
)
Something like this should work just rename the array names because you didn't provide them. But I think you'll get the idea :) Mainly you loop through all subcategories with foreach loop or another you'll get the parent id and can access the main array with that parent id and save the sub categories info in there.
foreach( $sub_array as $item ) {
$main_array[ category_id ][ $item[ 'parent_id' ] ][ 'subsubcategories' ] = $item;
}