2016-07-20 17 views
3

Ich habe API, die wie unten zurück:PHP - foreach überlappende Ausgabe

$arrays = array(
    "1" => array(
     "name" => "Mike", 
     "date" => "1/2/2016", 
     "criterion" => array(
      "1" => array(
        "label" => "Value for Money", 
        "scores" => "5" 
       ), 
      "2" => array(
        "label" => "Policy Features And Benefits", 
        "scores" => "1.5" 
       ), 
      "3" => array(
        "label" => "Customer Service", 
        "scores" => "3" 
       ) 

     ) 
    ), 
    "2" => array(
     "name" => "Megu", 
     "date" => "1/2/2015", 
     "criterion" => array(
      "1" => array(
        "label" => "Value for Money", 
        "scores" => "2" 
       ), 
      "2" => array(
        "label" => "Policy Features And Benefits", 
        "scores" => "3.5" 
       ), 
      "3" => array(
        "label" => "Customer Service", 
        "scores" => "1" 
       ) 

     ) 
    ) 
); 

Und PHP-Code:

$output = ''; 
$output_criterion = ''; 

foreach($arrays as $a){ 

    $criterions_arr = $a['criterion']; 
    foreach($criterions_arr as $c){ 
     $output_criterion .= $c['label'] . ' - ' . $c['scores'] . '<br/>'; 
    } 

    $output .= $a['name'] . '<br/>' . $output_criterion . '<br/>'; 

} 

echo $output; 

Ergebnis:

Mike 
Value for Money - 5 
Policy Features And Benefits - 1.5 
Customer Service - 3 

Megu 
Value for Money - 5 
Policy Features And Benefits - 1.5 
Customer Service - 3 
Value for Money - 2 
Policy Features And Benefits - 3.5 
Customer Service - 1 

Jedoch habe ich das Ergebnis möchte wie unten ohne Überlappung in der verschachtelten foreach Schleife:

Mike 
Value for Money - 5 
Policy Features And Benefits - 1.5 
Customer Service - 3 

Megu 
Value for Money - 2 
Policy Features And Benefits - 3.5 
Customer Service - 1 

Wie kann ich das tun, ich habe array_unique, aber es scheint nur mit 'Label' nicht die 'Scores' zu arbeiten.

Vielen Dank im Voraus

Antwort

3

variable $output_criterion in jeder äußeren Iteration zurücksetzen. Seine Verkettung aller vorherigen Werte.

$output = ''; 
foreach($arrays as $a){ 
    $output_criterion = ''; 
    $criterions_arr = $a['criterion']; 
    foreach($criterions_arr as $c){ 
     $output_criterion .= $c['label'] . ' - ' . $c['scores'] . '<br/>'; 
    } 
    $output .= $a['name'] . '<br/>' . $output_criterion . '<br/>'; 
} 
echo $output; 

Hinzufügen von Live-Demo: https://eval.in/608400

+1

Gute Antwort die Änderungen erklärt. Jedenfalls +1 um herauszufinden, – Thamilan

+0

Vielen Dank, so einfach! habe das nicht bemerkt! – Mike

0

einfach die output_criterion in die erste foreach-Schleife $ bewegen sich wie so:

<?php 

     $output     = ''; 

     foreach($arrays as $a){ 
      $criterions_arr  = $a['criterion']; 
      $output_criterion = ''; 
      foreach($criterions_arr as $c){ 
       $output_criterion .= $c['label'] . ' - ' . $c['scores'] . '<br/>'; 
      } 
      $output    .= $a['name'] . '<br/>' . $output_criterion . '<br/>'; 


     } 

     echo $output;