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
Gute Antwort die Änderungen erklärt. Jedenfalls +1 um herauszufinden, – Thamilan
Vielen Dank, so einfach! habe das nicht bemerkt! – Mike