Oliver2000
asked on
filter out values and recalculate average inside php array?
Hi experts,
I have a problem which is pretty much impossible for me to solve alone so I need your help.
I have an array of objects like this:
This array contains rating data from one object with creating date and rating value.
What I need to do now is to get somehow 2 functions which does the following:
Function1:
Need to check the array for ratings made on the SAME DAY and if this is the case combine them, calculate the average out of this ratings and write them back to the array in place of the existing ones before from the same day.
Ex.: If there would be a rating of 100 at 01.01.2015 and another rating of 50 at 01.01.2015 delete the first on and change the second into 75 (combine both and divide in 2 for 2 ratings)
Function2:
Is very similar and need to check the array for ratings made on the SAME MONTH and if this is the case combine them, calculate the average out of this ratings and write them back to the array in place of the existing ones before from the same month.
Ex.: If there would be a rating of 100 at 02.Feb.2015 and another rating of 50 at 01.Feb.2015 delete the first on and change the second into 75 (combine both and divide in 2 for 2 ratings)
I am pretty much without any idea how to solve this and appreciate your help here experts. Thanks a lot in advance.
I have a problem which is pretty much impossible for me to solve alone so I need your help.
I have an array of objects like this:
Array ( [0] => stdClass Object ( [created] => 2015-03-30 12:01:29 [avg_rating] => 50 ) [1] => stdClass Object ( [created] => 2015-03-31 06:59:09 [avg_rating] => 90 ) [2] => stdClass Object ( [created] => 2015-04-05 11:46:55 [avg_rating] => 93 ) [3] => stdClass Object ( [created] => 2015-04-06 06:53:32 [avg_rating] => 87 ) [4] => stdClass Object ( [created] => 2015-04-09 20:33:15 [avg_rating] => 89 ) [5] => stdClass Object ( [created] => 2015-04-10 17:53:59 [avg_rating] => 84 ) [6] => stdClass Object ( [created] => 2015-04-18 16:52:58 [avg_rating] => 86 ) )
This array contains rating data from one object with creating date and rating value.
What I need to do now is to get somehow 2 functions which does the following:
Function1:
Need to check the array for ratings made on the SAME DAY and if this is the case combine them, calculate the average out of this ratings and write them back to the array in place of the existing ones before from the same day.
Ex.: If there would be a rating of 100 at 01.01.2015 and another rating of 50 at 01.01.2015 delete the first on and change the second into 75 (combine both and divide in 2 for 2 ratings)
Function2:
Is very similar and need to check the array for ratings made on the SAME MONTH and if this is the case combine them, calculate the average out of this ratings and write them back to the array in place of the existing ones before from the same month.
Ex.: If there would be a rating of 100 at 02.Feb.2015 and another rating of 50 at 01.Feb.2015 delete the first on and change the second into 75 (combine both and divide in 2 for 2 ratings)
I am pretty much without any idea how to solve this and appreciate your help here experts. Thanks a lot in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$array=Array (
'0' => (object) array( 'created' => '2015-03-30 12:01:29', 'avg_rating' => 50 ),
'1' => (object) array( 'created' => "2015-03-31 06:59:09", 'avg_rating' => 90 ),
'2' => (object) array( 'created' => "2015-04-05 11:46:55", 'avg_rating' => 93 ),
'3' => (object) array( 'created' => "2015-04-06 06:53:32", 'avg_rating' => 87 ),
);
array_unshift($array, (object) array( 'created' => '2015-01-01 00:00:00', 'avg_rating' => 100 ));
'0' => (object) array( 'created' => '2015-03-30 12:01:29', 'avg_rating' => 50 ),
'1' => (object) array( 'created' => "2015-03-31 06:59:09", 'avg_rating' => 90 ),
'2' => (object) array( 'created' => "2015-04-05 11:46:55", 'avg_rating' => 93 ),
'3' => (object) array( 'created' => "2015-04-06 06:53:32", 'avg_rating' => 87 ),
);
array_unshift($array, (object) array( 'created' => '2015-01-01 00:00:00', 'avg_rating' => 100 ));
ASKER
Perfect solution and very fast reply, including my additional problem is solved. Thank you @OZO for this great solution. It does perfect solve my problem and I cant thank you enough.
ASKER
Can you help me with one more thing. I would like to push at the beginning of this same array now one manual entry. I tried array_unshift but somehow I have problems because of the object inside the array and I dont know the correct syntax.
So lets say I have this array
$array=Array (
'0' => (object) array( 'created' => '2015-03-30 12:01:29', 'avg_rating' => 50 ),
'1' => (object) array( 'created' => "2015-03-31 06:59:09", 'avg_rating' => 90 ),
'2' => (object) array( 'created' => "2015-04-05 11:46:55", 'avg_rating' => 93 ),
'3' => (object) array( 'created' => "2015-04-06 06:53:32", 'avg_rating' => 87 ),
);
and I would like to push at the first spot now 'created' => '2015-01-01 00:00:00', 'avg_rating' => 100
so the end result would be
$array=Array (
'0' => (object) array( 'created' => '2015-01-01 00:00:00', 'avg_rating' => 100 ),
'1' => (object) array( 'created' => '2015-03-30 12:01:29', 'avg_rating' => 50 ),
'2' => (object) array( 'created' => "2015-03-31 06:59:09", 'avg_rating' => 90 ),
'3' => (object) array( 'created' => "2015-04-05 11:46:55", 'avg_rating' => 93 ),
'4' => (object) array( 'created' => "2015-04-06 06:53:32", 'avg_rating' => 87 ),
);
Thank you so much for your help.
PS: Of course I would not create the array here in this place and have a ready array where I need to push at the beginning one entry manually)