2016-08-19 2 views
0

Bitte beachten Sie folgende Array in PHP:Wie fasst man Werte eines anderen Schlüssels zusammen, wenn der gleiche Wert für einen anderen Schlüssel in einem assoziativen Array in PHP gilt?

$array1 = array(
    array('location'=>'AA','time_spent'=>2), 
    array('location'=>'BB','time_spent'=>2), 
    array('location'=>'AA','time_spent'=>3), 
    array('location'=>'CC','time_spent'=>2), 
    array('location'=>'CC','time_spent'=>2), 
    array('location'=>'AA','time_spent'=>1) 
); 

Hier bin total Mal, wenn er in jedem bestimmten Ort verbracht, das heißt zu akkumulieren Wert von Schlüssel time_spent, wenn der Schlüssel für location gleiche berechnet werden soll. Und dann, um es in einem neuen assoziativen Array aufzuzeichnen (zB $place_array) mit Werten, die den Schlüsseln location und total_time zugeordnet sind.

So sollte die Ausgabe Array

sein
$place_array = array(
    array('location'=>'AA','time_spent'=>6), 
    array('location'=>'BB','time_spent'=>2), 
    array('location'=>'CC','time_spent'=>4) 
); 
+2

Bitte aktualisieren Sie Ihre Frage alle Versuche schließen Sie haben (und Fehler mit diesen Versuchen) gemacht, um das gewünschte Ergebnis zu erreichen – Epodax

+0

@Epodax, Sicher, ich werde dafür sorgen, es in der nächsten Frage zu tun. Sorry, ich habe die vorherigen Versuche nicht, da ich bereits mit den gegebenen Antworten ersetzt habe. Danke trotzdem für die inkrementelle Rückmeldung. – Wenuka

Antwort

1

Meine Antwort zeigt Ihnen, wie ich das Array erhalten würden Sie wollten aber auch die tatsächliche Array würde ich verwenden, wenn ich es war Codierung . Wenn alles, was ich wollte, eine Zusammenfassung der Standorte und der Gesamtzeit war, würde ich mit einem kompakteren Array arbeiten, das die Labels nicht weiter speichern musste. Wie auch immer, wenn Sie die Zusammenfassung Bit nicht wollen, nehmen Sie es nur, wenn Sie diese Version denken kompakter ist :)

$journey = array(
    array('location'=>'AA','time_spent'=>2), 
    array('location'=>'BB','time_spent'=>2), 
    array('location'=>'AA','time_spent'=>3), 
    array('location'=>'CC','time_spent'=>2), 
    array('location'=>'CC','time_spent'=>2), 
    array('location'=>'AA','time_spent'=>1) 
); 

$summary = Array(); 
$full = Array(); 
foreach($journey as $map){ 

    // Your version 
    if(!isset($full[$map["location"]])){ 
     $full[$map["location"]] = $map; 
    } else { 
     $full[$map["location"]]["time_spent"] += $map["time_spent"]; 
    } 

    // Summary version (leaner) 
    $summary[$map["location"]] = (isset($summary[$map["location"]])?$summary[$map["location"]]:0)+$map["time_spent"]; 
} 

// Summary result 
print "<pre>"; 
print_r($summary); 
print "</pre>"; 

// Your result 
print "<pre>"; 
print_r(array_values($full)); 
print "</pre>"; 

Ausgabe

Array 
(
    [AA] => 6 
    [BB] => 2 
    [CC] => 4 
) 

Array 
(
    [0] => Array 
     (
      [location] => AA 
      [time_spent] => 6 
     ) 

    [1] => Array 
     (
      [location] => BB 
      [time_spent] => 2 
     ) 

    [2] => Array 
     (
      [location] => CC 
      [time_spent] => 4 
     ) 

) 
1
<?php 

$array1 = array(
    array('location'=>'AA','time_spent'=>2), 
    array('location'=>'BB','time_spent'=>2), 
    array('location'=>'AA','time_spent'=>3), 
    array('location'=>'CC','time_spent'=>2), 
    array('location'=>'CC','time_spent'=>2), 
    array('location'=>'AA','time_spent'=>1) 
); 

$place_array = array(); 
foreach ($array1 as $key => $value) { 
    if(isset($place_array[$value['location']])) 
     $place_array[$value['location']]['time_spent'] += $value['time_spent']; 
    else 
     $place_array[$value['location']] = $value; 
} 
$place_array = array_values($place_array); 
echo '<pre>'; 
print_r($place_array); 
echo '</pre>'; 
?> 

Ausgabe

Array 
(
    [0] => Array 
    (
     [location] => AA 
     [time_spent] => 6 
    ) 

    [1] => Array 
    (
     [location] => BB 
     [time_spent] => 2 
    ) 

    [2] => Array 
    (
     [location] => CC 
     [time_spent] => 4 
    ) 
) 
1

Hallo Sie können versuchen diese.

Ich habe ein neues Array $result mit seinem Schlüssel als Standort AA sagen erstellt und in das ich habe Array speichern, die Lage haben als AA und seine zeit verbrachte ich beim nächsten Mal überprüft mich, wenn Lage ist AA dann seine hinzufügen Zeitbedarf zum vorherigen und wenn nicht dann neu hinzufügen. Bei der letzten verwendet i array_values geändert Tasten

<?php 
    $array1 = array(array('location'=>'AA','time_spent'=>2),array('location'=>'BB','time_spent'=>2),array('location'=>'AA','time_spent'=>3),array('location'=>'CC','time_spent'=>2),array('location'=>'CC','time_spent'=>2),array('location'=>'AA','time_spent'=>1)); 
    $result = array(); 
    foreach($array1 as $new){ 
    if (array_key_exists($new['location'],$result)){ 
     $result[$new['location']]['time_spent'] += $new['time_spent']; 
     } 
    else{ 
     $result[$new['location']] = $new; 
     } 
    } 
    $final = $new_array=array_values($result); 
    echo "<pre>";print_r($final); 

Ausgabe

Array 
    (
     [0] => Array 
      (
       [location] => AA 
       [time_spent] => 6 
      ) 

     [1] => Array 
      (
       [location] => BB 
       [time_spent] => 2 
      ) 

     [2] => Array 
      (
       [location] => CC 
       [time_spent] => 4 
      ) 

    ) 
Verwandte Themen