<?php
$string = '{
"status": {
"message": "success",
"code": 200
},
"clients": [
{
"values": [
{
"Code": "rdf",
"name": "first"
},
{
"Code": "fg",
"name": "second"
}
],
"name": "Customers"
},
{
"values": [
{
"Code": "fgf",
"name": "third"
},
{
"Code": "api",
"name": "4th"
},
{
"Code": "fgbb",
"name": "5th"
},
{
"Code": "acy",
"name": "last"
}
],
"name": "Lead"
}
]
}';
$data = json_decode($string,true);
echo "<Select>";
foreach ($data['clients'] as $c) {
# scope is $c['scope']
$gp =$c['name'];
echo "<optgroup label=$gp>";
//echo "Starting " . $c['name'];
# calling the function on the categories data
recurse($c['values']);
echo '</optgroup>';
//echo "Finished " . $c['scope'] . PHP_EOL;
}
function recurse($arr, $level = 0){
# we have a numerically-indexed array. go through each item:
foreach ($arr as $n) {
# print out the item ID and the item name
echo '<option value="' . $n['Code'] . '">'
. str_repeat("-", $level)
. $n['name']
. '</option>'
;
}
}
echo "</Select>";
?>