here is my json data
{ "order_id":"#BCB28FB2", "salutation":"Mr", "name":"Testing Data", "cart":[ { "id":13, "name":"tes1", "treatment_type":"tes", }, { "id":14, "name":"tes2", "treatment_type":"tes", }, { "id":15, "name":"Panel", "treatment_type":"panel", }, { "id":16, "name":"Paket", "treatment_type":"paket", }, ] }
and this my code
foreach($data['cart'] as $value){
if($value['treatment_type'] == 'tes'){
echo "Its a tes<br>";
}
elseif($value['treatment_type'] == 'panel'){
echo "Its a panel<br>";
}else{
echo "Its a paket<br>";
}
}
my site showing
enter image description here how to show like this
You may use array_unique
to do the job
So the code is
<?php
$json='{ "order_id":"#BCB28FB2", "salutation":"Mr", "name":"Testing Data", "cart":[ { "id":13, "name":"tes1", "treatment_type":"tes" }, { "id":14, "name":"tes2", "treatment_type":"tes" }, { "id":15, "name":"Panel", "treatment_type":"panel" }, { "id":16, "name":"Paket", "treatment_type":"paket" } ] }';
$data=json_decode($json, true);
foreach($data['cart'] as $value){
$data2[]=$value['treatment_type'];
}
$data3=array_unique($data2);
foreach($data3 as $value){
if($value == 'tes'){
echo "Its a tes<br>";
}
elseif($value == 'panel'){
echo "Its a panel<br>";
}else{
echo "Its a paket<br>";
}
}
?>
Actually for the foreach loop, you may further simplify to the following:
foreach($data3 as $value){
echo "Its a " . $value . "<br>";
}
You may check the execution result at the following sandbox URL:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.