2016-12-24 2 views
-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))) 

Wenn Sie sehen, Hydraulik-Array Lektion1 erscheint 2 mal. Ich möchte den 1. Positionswert der Lektion 1 hinzufügen und andere doppelte Einträge löschen. Ich möchte, dass die Daten an Google charts.I einige Array-Teil entfernt füttern, da es zu lang war.Php Array-Wert der 1. Position hinzufügen, wenn die 0. Position

+0

Bitte beachten Sie diesen Link von hier aus können Sie Antwort ...... bekommen http://stackoverflow.com/questions/1496682/how-to-Summe-Werte-of-the-Array-of-the-same-Taste – Shibon

+0

Wenn Sie duplizierte Werte entfernen möchten, verwenden Sie [array_unique] (http://php.net/manual/en/ function.array-unique.php), entfernt doppelte Werte aus einem Array. – matiaslauriti

+0

aber ich habe Wert der ersten Position addieren und dann andere Array entfernen. [0] => Lesson1 [1] => 1 [3] => Array ([0] => Lesson1 [1] => 1) –

Antwort

0

Sie können einfach Schleife durch das Array gleichen Werte zu finden, und Sie können auch hinzufügen und entfernen sie, überprüfen Sie den Code zu verstehen, ich hoffe, dies wird für Sie arbeiten.

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

Diese Sie geben:

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

Verwandte Themen