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:

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 ) ) 

Open in new window


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.
Oliver2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
function Function1($array){
  foreach( $array as $key => $value ){
    $k=substr($value->created,0,10);
    $a[$k]->total +=$value->avg_rating;
    $a[$k]->count+=1;
    $a[$k]->created=$value->created;
    $a[$k]->key=$key;
  }
  foreach( $a as $key => $value ){
    $b[$value->key]=(object) array( 'created' => $value->created, 'avg_rating' => $value->total/=$value->count);
  }
  return $b;
}
function Function2($array){
  foreach( $array as $key => $value ){
    $k=substr($value->created,0,7);
    $a[$k]->total +=$value->avg_rating;
    $a[$k]->count+=1;
    $a[$k]->created=$value->created;
    $a[$k]->key=$key;
  }
  foreach( $a as $key => $value ){
    $b[$value->key]=(object) array( 'created' => $value->created, 'avg_rating' => $value->total/=$value->count);
  }
  return $b;
}
$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 ),
'4' => (object) array( 'created' => "2015-04-09 20:33:15", 'avg_rating' => 89 ),
'5' => (object) array( 'created' => "2015-04-10 17:53:59", 'avg_rating' => 84 ),
'6' => (object) array( 'created' => "2015-04-18 16:52:58", 'avg_rating' => 86 ),
'7' => (object) array( 'created' => "2015-01-01", 'avg_rating' => 100 ),
'8' => (object) array( 'created' => "2015-01-01", 'avg_rating' => 50 ),
'9' => (object) array( 'created' => "2015-02-01", 'avg_rating' => 100 ),
'10' => (object) array( 'created' => "2015-02-01", 'avg_rating' => 50 ),
);
var_dump($array);
$array=Function1($array);
var_dump($array);
$array=Function2($array);
var_dump($array);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Oliver2000Author Commented:
@OZO you are a live safer, it works absolute perfect. you saved my week!

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)
0
ozoCommented:
$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
Oliver2000Author Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.