ich ein Array von Daten von AngularJS zu PHP bin vorbei, und ich mit dem folgende Array am Ende:Echo Werte in verschachtelten Array in PHP
if(in_array('application/json', $contentType)) { // Check if Content-Type is JSON
$data = json_decode($rawBody); // Then decode it
$productes = $data->products;
} else {
parse_str($data, $data); // If not JSON, just do same as PHP default method
}
products:[
0:{
product: "Name1",
number: 1
},
1:{
product: "Name2",
number: 3
}
]
Wie könnte ich eine Schleife verwalten durch sie bis Ende eine Liste der Produkte Anzeigen wie folgt:
<li>Name1 (1)</li>
<li>Name2 (3)</li>
ich habe folgendes aber kein Glück versucht:
foreach ($products as $value) {
echo "<li>". $value->product ." (". $value->number .")</li>";
}
Dies ist, was ich erhalten, wenn ich var_dump ($ products):
array(4) {
[0]=>
object(stdClass)#2 (3) {
["product"]=>
string(19) "Croissant d'ametlla"
["number"]=>
int(1)
["preu"]=>
float(1.95)
}
[1]=>
object(stdClass)#3 (3) {
["product"]=>
string(29) "Pain au chocolat (napolitana)"
["number"]=>
int(1)
["preu"]=>
float(1.4)
}
[2]=>
object(stdClass)#4 (3) {
["product"]=>
string(16) "Brioche de sucre"
["number"]=>
int(1)
["preu"]=>
float(1.2)
}
[3]=>
object(stdClass)#5 (3) {
["product"]=>
string(36) "Pa de blat egipci i integral (Xusco)"
["number"]=>
int(1)
["preu"]=>
float(4.45)
}
}
SOLUTION
Wie ich bereits intially die JSON Decodierung, das ist, was Arbeit endete:
foreach ($products as $product) {
echo "<li>". $product->product ." (". $product->number .")</li>";
};
bitte versuchen Sie es zu 'var_dump ($ Produkte)' und teilen Sie das Ergebnis – Mohammad
@Mohammad zu sehen edit:) –
Wenn Sie sagen, "... aber kein Glück" , welche Ausgabe hast du bekommen? Gab es einen Fehler? Hat sich irgendetwas gezeigt? – IMSoP