Praetorian46 Posted December 27, 2007 Share Posted December 27, 2007 OK what I'm am trying to do is basically remove duplicate entries in one array. Then where ever there was a duplicate value. So if ArrayID(3) was a duplicate of ArrayID(0) then I want to add ArrayAmount(3) and ArrayAmount(0) together... but I want this to be able to work at a large scale. there may be 1000 values in the array and 50 IDs are the same. Here is what I've come up with so far but I'm getting an error. The code is probably messy since I'm fairly new to PHP. <?php //$JI is an array with allot of "Job IDs" in it //$BA is an array with allot of "Billing Amounts" in it //Both arrays will always be the same length $NewBA=array(''); $NewJI=array(''); for($x=0; $x <=(count($JI)-1); $x++){ for($y=0; $y<=(count($JI)-1); $y++){ if($JI($x)==$JI($y) and $x!=$y and $y>$x){ if($NewBA($x)==0)$NewBA($x)=$BA($x); //*See ERROR below $NewBA($x)+=$BA($y); $NewJI($x)=$JI($x); } } } ?> Here is the error I get ERROR: PHP Fatal error: Can't use function return value in write context ??? I'm not sure where to go from here since the code errors out before I can even debug it. Any help would be appreciated thank you very much. -Cody Quote Link to comment https://forums.phpfreaks.com/topic/83369-solved-consolidating-an-array-while-adding-anothers-values/ Share on other sites More sharing options...
Psycho Posted December 27, 2007 Share Posted December 27, 2007 Well, thew first problem I see (which is probably causing your error) is that you are not referencing the array's correctly. $NewBA($x) //Incorrect format $NewBA[$x] //Correct format That being said, I think there is a more efficient solution: <?php $array_sums = array(); foreach ($ArrayID as $key => $id_value) { $array_sums[$id_value] += $ArrayAmount[$key]; } ?> When that completes you will hav a new array ($array_sums) with the following format: array ( 'id1' => summedvaluesforid1, 'id2' => summedvaluesforid2, 'id3' => summedvaluesforid3 . . . Quote Link to comment https://forums.phpfreaks.com/topic/83369-solved-consolidating-an-array-while-adding-anothers-values/#findComment-424175 Share on other sites More sharing options...
Praetorian46 Posted December 28, 2007 Author Share Posted December 28, 2007 Hey thanks a bunch... That worked perfectly! And that code is allot more simple to read and understand. Thank you, Cody Quote Link to comment https://forums.phpfreaks.com/topic/83369-solved-consolidating-an-array-while-adding-anothers-values/#findComment-424765 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.