-1
Array ( [Hydraulics] => Array ( [0] => Array ( [0] => Lesson1 [1] => 1 ) [1] => Array ( [0] => Lesson3 [1] => 1 ) [3] => Array ( [0] => Lesson1 [1] => 1 ) [4] => Array ( [0] => Lesson2 [1] => 1 ) [5] => Array ( [0] => Lesson3 [1] => 1 ) ) [Waste Water Engineering] => Array ( [0] => Array ( [0] => Lesson1 [1] => 1 ) [1] => Array ( [0] => Lesson2 [1] => 1 ) [2] => Array ( [0] => Lesson3 [1] => 0 ) ) [RCC Structure Design] => Array ( [0] => Array ( [0] => Lesson1 [1] => 1 ) [1] => Array ( [0] => Lesson2 [1] => 1 ) [2] => Array ( [0] => Lesson3 [1] => 1 ) ) [Irrigation] => Array ( [0] => Array ( [0] => Lesson1 [1] => 0 ) [1] => Array ( [0] => Lesson2 [1] => 1 ) [2] => Array ( [0] => Lesson3 [1] => 1 ) ) [Plastic Blocks] => Array ( [0] => Array ( [0] => Lesson1 [1] => 1 ) [1] => Array ( [0] => Lesson2 [1] => 1 ) [2] => Array ( [0] => Lesson3 [1] => 1 ) ) )

If you see Hydraulics array lesson1 appears 2 times. I want to add Lesson1 1st position value to be added and delete other duplicate entries. I want to feed the data to google charts.I have removed some array part as it was too long.

Abhishek kadadi
  • 120
  • 2
  • 11
  • Please refer this link may be you get answer from here...... http://stackoverflow.com/questions/1496682/how-to-sum-values-of-the-array-of-the-same-key – Shibon Dec 24 '16 at 06:07
  • If you want to remove duplicated values, use [array_unique](http://php.net/manual/en/function.array-unique.php), it will remove duplicate values from an array. – matiaslauriti Dec 24 '16 at 04:38
  • but I have to add value of 1st position and then remove other array. [0] => Lesson1 [1] => 1, [3] => Array ( [0] => Lesson1 [1] => 1 ) – Abhishek kadadi Dec 24 '16 at 04:40
  • I did not understand – matiaslauriti Dec 24 '16 at 04:41
  • as lesson1 appears 2 times in [Hydraulics] => Array ( [0] => Array ( [0] => Lesson1 [1] => 1 ) [3] => Array ( [0] => Lesson1 [1] => 1 ) I have to add the values of these 2 array for Lesson1 and then remove any duplicate array for Lesson1 – Abhishek kadadi Dec 24 '16 at 04:44

1 Answers1

0

You can simply loop through the array to find same values and you can also add and remove them, check the code below to understand, I hope this will work for you.

<?php 
$array['Hydraulics'] = array ( 
                            0 => array ( 0 => 'Lesson1', 1 => 1 ),
                            1 => array ( 0 => 'Lesson3', 1 => 1 ),
                            3 => array ( 0 => 'Lesson1', 1 => 1 ), 
                            4 => array ( 0 => 'Lesson2', 1 => 1 ),
                            5 => array ( 0 => 'Lesson3', 1 => 1 ) 
                        );

$checked_keys=array(); //array to store checked keys.
foreach($array['Hydraulics'] as $key1 =>$val1){  ///first loop
    $string1 = $val1[0];  //value at key 0 for each node eg. Lesson1,Lesson3 etc
    foreach($array['Hydraulics'] as $key2 => $val2){ ///again loop the same array for finding same values
        $string2 = $val2[0]; //value at key 0 for each node eg. Lesson1,Lesson3 etc
        if($string1==$string2 && $key2 != $key1 && !in_array($key2,$checked_keys)){ //will go further only value matches and key of first loop != second loop
            $array['Hydraulics'][$key1][1] =  $val1[1]+$val2[1]; //add the values and index 1.
            $checked_keys[]= $key1; ///push chekced keys in array for skipping next time.
            unset($array['Hydraulics'][$key2]); //unset the duplicate values.
        }   
    }
}       
echo "<pre>";print_r($array);//output                        
?>

This will give you :

Array
(
    [Hydraulics] => Array
        (
            [0] => Array
                (
                    [0] => Lesson1
                    [1] => 2
                )

            [1] => Array
                (
                    [0] => Lesson3
                    [1] => 2
                )

            [4] => Array
                (
                    [0] => Lesson2
                    [1] => 1
                )

        )

)

CLICK HERE FOR LIVE DEMO

Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20