Merge array

Arrays:

$ab=array( 
	array('rsname' =>153584 ,'avg'=>46,'claim'=>15),
	array('rsname'=>153589,'avg'=>58,'claim'=>20),
	array('rsname'=>153545,'avg'=>55,'claim'=>25),
	array('rsname'=>153590,'avg'=>45,'claim'=>15)
	);
$bc=array(
	array('rsname'=>153584,'avg'=>40,'claim'=>45),
	array('rsname'=>153570,'avg'=>52,'claim'=>36),
	array('rsname'=>153589,'avg'=>31,'claim'=>05),
	array('rsname'=>153517,'avg'=>18,'claim'=>85)
	);

Desire output:

Array
(
    [0] => Array
        (
            [rsname] => 153584
            [claim] => 60
            [avg] => 43
        )

    [1] => Array
        (
            [rsname] => 153589
            [claim] => 25
            [avg] => 45
        )
    [2] => Array
        (
            [rsname] => 153517
            [claim] => 85
            [avg] => 18
        )
        [3] => Array
        (
            [rsname] => 153570
            [claim] => 36
            [avg] => 52
        )

        [4] => Array
        (
            [rsname] => 153545
            [claim] => 25
            [avg] => 55
        )
        [5] => Array
        (
            [rsname] => 153590
            [claim] => 15
            [avg] => 45
        )
)

If same rsname id found in both arrays ,calculate average of avg column and sum of claims column and add reset of array .Plz help
Thanks you.

and what did you try by yourself, so that others do not have to make the whole work for you?

1 Like

I don’t think there’s a built-in PHP function to do this (it’s a pretty specific requirement) so all you can do is write some code to run through one of the arrays, check the other array, and then deal with what you find.

// loop through array
// is entry in second array? 
//   yes - add claim, adjust avg
//   no - append to second array
// end of loop

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.