PHP array reorganize with unique count - php

I have following array which has ID as index, and some count as value. Ultimate goal is to get total of unique IDs in another array.
Array
(
[0] => Array
(
[0] => Team Object
(
[id] => 1
[countStat] => 25
)
)
[1] => Array
(
[0] => Team Object
(
[id] => 2
[countStat] => 24
)
)
[2] => Array
(
[0] => Team Object
(
[id] => 1
[countStat] => 23
)
)
[3] => Array
(
[0] => Team Object
(
[id] => 3
[countStat] => 23
)
)
[4] => Array
(
[0] => Team Object
(
[id] => 5
[countStat] => 21
)
)
[5] => Array
(
[0] => Team Object
(
[id] => 3
[countStat] => 21
)
)
[6] => Array
(
[0] => Team Object
(
[id] => 5
[countStat] => 20
)
)
[7] => Array
(
[0] => Team Object
(
[id] => 2
[countStat] => 20
)
)
)
I want result like below.
Array
(
[0] => Array
(
[0] => Team Object
(
[id] => 1
[countStat] => 48
)
)
[1] => Array
(
[0] => Team Object
(
[id] => 2
[countStat] => 44
)
)
[3] => Array
(
[0] => Team Object
(
[id] => 3
[countStat] => 44
)
)
[4] => Array
(
[0] => Team Object
(
[id] => 5
[countStat] => 41
)
)
)
I has spend few hours working on it but couldn't get solution. Can someone please help ?
Thank you.

Looks like you want to sum team object's countStats for like ids.
I've created a similar data structure, looped through it to form a temporary array that uses the id as a key, with associated amounts (to later sum - mine is amount, yours is countStats).
Then sum those and recreate desired output.
<?php
class Team
{
public $id;
public $amount;
public function __construct($id, $amount)
{
$this->id = $id;
$this->amount = $amount;
}
}
$input =
[
[new Team(1, 3)],
[new Team(1, 4)],
[new Team(2, 5)],
[new Team(2, 7)]
];
foreach($input as $subarray)
$amounts[$subarray[0]->id][]=$subarray[0]->amount;
print_r($amounts);
foreach($amounts as $k => $v)
$result[] = [new Team($k, array_sum($v))];
print_r($result);
Output:
Array
(
[1] => Array
(
[0] => 3
[1] => 4
)
[2] => Array
(
[0] => 5
[1] => 7
)
)
Array
(
[0] => Array
(
[0] => Team Object
(
[id] => 1
[amount] => 7
)
)
[1] => Array
(
[0] => Team Object
(
[id] => 2
[amount] => 12
)
)
)

Related

Filter multidimensional php array with another array

I have an array $result as such:
[0] => Array (
[0] => Array (
[itemid] => 1
[name] => A
)
[1] => Array (
[itemid] => 2
[name] => B
)
)
[1] => Array (
[0] => Array (
[itemid] => 3
[name] => C
)
[1] => Array (
[itemid] => 2
[name] => B
)
)
and an array $items as such:
[0] => Array (
[itemid] => 2
[name] => B
)
[1] => Array (
[itemid] => 4
[name] => D
)
How do I remove all items from the $result array, that occur in the $items array? In this case, the $result would become:
[0] => Array (
[0] => Array (
[itemid] => 1
[name] => A
)
)
[1] => Array (
[0] => Array (
[itemid] => 3
[name] => C
)
)
Since the question is mostly code, here's some extra characters to make StackOverflow accept the question.
I think this is what you want. (Not tested yet)
<?php
foreach ($result as $key => $array) {
$result[$key] = array_diff($array, $items);
}
print_r($result);

How to merge array using same id

I have two arrays and I want to combine them together
1) first look like this:
[11] => Array
(
[id] => 11
[name] => test
)
[12] => Array
(
[id] => 12
[name] => test1
)
2) second array look like this:
[0] => Array
(
[offer_id] => 11
[countries] => Array
(
[SA] => Array
(
[id] => 682
)
)
)
[1] => Array
(
[offer_id] => 12
[countries] => Array
(
[KW] => Array
(
[id] => 414
)
)
)
I want this result. How is it possible can any one provide solution for same?
[11] => Array
(
[id] => 11
[name] => test
[countries] => Array
(
[SA] => Array
(
[id] => 682
)
)
)
[12] => Array
(
[id] => 12
[name] => test
[countries] => Array
(
[KW] => Array
(
[id] => 414
)
)
)
Thank you for the help!
Try this:
foreach ($array1 as &$arr1) {
$offer_id = $arr1['id']; // Search for this offer_id in array 2
$match = array_filter($array2, function($v) use ($offer_id){
return $v['offer_id'] == $offer_id; // Return matching offer id
});
$arr1['countries'] = current($match)['countries']; // Assign matched country to array
}

sorting 2D-Array in PHP

I have the following 2D-Array:
Array (
[ID] => Array
(
[0] => 150
[1] => 250
)
[Group] => Array
(
[0] => 120
[1] => 120
)
[Name] => Array
(
[0] => Name 1
[1] => Name 2
)
[Price] => Array
(
[0] => 9.99
[1] => 11.99
)
[Date] => Array
(
[0] => 12.04.2013
[1] => 11.04.2013
)
[SortIndex] => Array
(
[0] => 20
[1] => 10
)
)
and I want to sort them after the SortIndex-Array ascending. But the different Values from the Arrays are related to the others. So the association can't be changed.
How I could sort them?
Is there is any reason not to convert array to more suitable form?
Array (
[0] => Array
(
[ID] => 150
[Group] => 120
)
[1] => Array
(
[ID] => 250
[GROUP] => 120
)
....

Rebuild php array structure

I've been looking for the follow solution to create the array structure I need.
For a a list with updates for a specific user who can be following different things, I need to get first the followed items and the data compare to this item. my output for this is as follow.
array how it is right now
Array
(
[0] => Array
(
[website_id] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
[1] => Array
(
[id] => 1
[follower_id] => 2
)
[2] => Array
(
[id] => 1
[follower_id] => 3
)
[3] => Array
(
[id] => 1
[follower_id] => 4
)
[4] => Array
(
[id] => 1
[follower_id] => 5
)
)
)
[1] => Array
(
[website_id] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
)
)
)
I want to create the above array like tho on below
array like it has to be
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
[1] => Array
(
[id] => 1
[follower_id] => 2
)
[2] => Array
(
[id] => 1
[follower_id] => 3
)
[3] => Array
(
[id] => 1
[follower_id] => 4
)
[4] => Array
(
[id] => 1
[follower_id] => 5
)
[5] => Array
(
[id] => 1
[follower_id] => 1
)
)
Thanks in advice.
With kind regards,
Nicky
Seems to me that array_merge() should be sufficient:
$newArray = array_merge($myArray[0]['website_id'], $myArray[1]['website_id']);

Merging two objects

Quotes object:
Array
(
[0] => Array
(
[sitecaptions] => Array
(
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras
and Camcorders
)
)
)
The Posts Object:
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array
(
[0] => Array
(
[id] => 99
[name] => Others
)
)
)
[1] => Array
(
[Post] => Array
(
[id] => 2798
[post_title] => xx2
[item_desc] => xx2 desc
[dateadded] => 2009-12-22 11:10:45
)
[Category] => Array
(
[0] => Array
(
[id] => 99
[name] => Others
)
)
)
)
As you can see, the Posts Object contains two elements, [Post] and
[Category] for each record [0],[1] etc. I want to insert the
[sitecaptions] element into that Posts Object so that in effect it
looks like:
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array
(
[0] => Array
(
[id] => 99
[name] => Others
)
)
[sitecaptions] => Array
(
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras
and Camcorders
)
)
)
How do I combine two objects like that? Or how do I insert elements
into an existing object? Hope I'm clear about what I'm asking. Thanks
for your time...
lets call these objects $Quotes and $Posts respectively.
Quotes object:
Array (
[0] => Array (
[sitecaptions] => Array (
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras and Camcorders )
)
)
)
The Posts Object:
Array (
[0] => Array (
[Post] => Array (
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array (
[0] => Array (
[id] => 99
[name] => Others
)
)
)
[1] => Array (
[Post] => Array (
[id] => 2798
[post_title] => xx2
[item_desc] => xx2 desc
[dateadded] => 2009-12-22 11:10:45
)
[Category] => Array (
[0] => Array (
[id] => 99
[name] => Others
)
)
)
)
do you want the [sitecaptions] from the $quotes to be in both of your $posts elements? or just the one with the same key?
as to say if you have just $quotes[0] only $posts[0] will be affected. OR both $posts[0] and $posts[1] will be affected.
if you want $quotes[0] to be in both $post elements you can do this:
foreach ($posts as $key=>$post) {
$posts[$key]['sitecaptions'] = $quotes[0]['sitecaptions'];
}
if you want only the elements from $quotes that have the same index as the elements in $posts you can do this:
$posts = array_merge_recursive($posts,$quotes);
doing this second one would have $posts[1] being without a ['sitecaptions'] element.
end result:
Array (
[0] => Array (
[Post] => Array (
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array (
[0] => Array (
[id] => 99
[name] => Others
)
)
[sitecaptions] => Array (
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras and Camcorders
)
)
)
Hope it helps!

Categories