Пожалуйста, проверьте это. Сначала я отделил products
, а затем повторил массив продуктов по отдельности.
<?php
$str = '{ "plans": [ "silver", { "plan_name": "Silver Plan", "products": ["A", "B"], "price": "1000" }, "gold", { "plan_name": "Gold Plan", "products": ["A", "B"], "price": "2000" } ], "products": [ "A", { "product_name": "Plan A", "price": "100" },
"B",
{
"product_name": "Plan B",
"price": "100"
},
"C",
{
"product_name": "Plan C",
"price": "100"
}
]
}';
$products = json_decode($str);
// print_r($products);
foreach($products as $key=>$value)
{
if($key=='products')
{
foreach ($value as $key1 => $value1) {
if(!is_object($value1))
echo $value1 . '<br>';//will echo A,B,C
if(is_object($value1))
{
foreach ($value1 as $key2 => $value2) {
echo $value2 . '<br>';//Will echo product_name and price
}
}
}
}
}
Результат
A
Plan A
100
B
Plan B
100
C
Plan C
100