[ create a new paste ] login | about

Link: http://codepad.org/vLhzs6kJ    [ raw code | output | fork ]

PHP, pasted on Nov 19:
<?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>";

?>


Output:
1
<Select><optgroup label=Customers><option value="rdf">first</option><option value="fg">second</option></optgroup><optgroup label=Lead><option value="fgf">third</option><option value="api">4th</option><option value="fgbb">5th</option><option value="acy">last</option></optgroup></Select>


Create a new paste based on this one


Comments: