0

I have an array with small example below

Array (2)
system => Array (1)
  system_id => 3
proc => Array (4)
  proc_id => 5
  proc_or => 1
  proc_owner => 7751
  results => Array (7)
    0 => Array (5)
      process_id => 101
      process => 1335
      process_name => xa
      process_owner => xo
      rating => 67.554
    1 => Array (6)
      process_id => 122
      process => 1335
      process_name => xa
      process_owner => xo
      rating => 33.554
      proc_rel => xf    
    2 => Array (5)
      process_id => 101
      process => 1227
      process_name => xd
      process_owner => xa
      rating => 123.78
    3 => Array (8)
      process_id => 101
      process => 1291
      process_name => xa
      process_owner => xo
      rating => 64.241
      proc_rel => xf
      proc_rel_id => 1474
      proc_rel_owner => xm

I need to be able to organise this so that it contains only unique process_id values, so results 2 and 3 would be removed and the rating for that remaining unique id is the sum of all ratings for the records with that id so the results 0 rating id would become the sum of results 0 2 and 3.

There are thousands of records so its not practyical to do it without some sort of automated loop

I was thinking of maybe creating a new array and as its being processed

If the process_id is not in new array add it and all related data If the process id is already in the array just add(+) the value of rating to the existing value.

I have tried to adapt a couple of loops that ive found but they dont seem to work properly.

Not sure if this is the way to go and Im not sure how to do it anyway so any suggestions greatly appreciated.

1
  • Where is the data being generated. If from a database then the query can be set adjust the output. Commented Aug 18, 2013 at 8:47

1 Answer 1

1

If the data set is big, then better try to adjust your database query or change file structure when its generated. If it isnt possible then the only rational way is to create new array:

$newArray = ();
foreach($array['proc']['results'] as $result) {
    if (isset($newArray[$result['process_id']]) {
        $newArray[$result['process_id']]['rating'] += $result['rating'];
    } else {
        $newArray[$result['process_id']] = $result;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.