mercredi 26 avril 2017

foreach iteration structure show

i have some script with foreach, see below, my for each always show output 2 - 1 - 3 - 4 - etc from my json object, how to solve my problem,?

<?php public function renderLayout3x3($data=array()){
    $output = "<ul>";

    foreach ($data as $key => $value) {
        if($value['posisi'] == '1'){
            $output .= "<li><ul>
                                <li>POS:".$value['posisi']."</li>
                                <li>HOSTNAME:".$value['hostname']."</li>
                        </ul></li>";
        }
        if($value['posisi'] == '2'){
            $output .= "<li><ul>
                                <li>POS:".$value['posisi']."</li>
                                <li>HOSTNAME:".$value['hostname']."</li>
                        </ul></li>";
        }
        if($value['posisi'] == '3'){
            $output .= "<li><ul>
                                <li>POS:".$value['posisi']."</li>
                                <li>HOSTNAME:".$value['hostname']."</li>
                        </ul></li>";
        }

        //for debug..
        print json_encode($value['posisi']);
        /*
        Output : "2","1","3","4","5","6","7","8","9"
        Why : 2 - 1 - 3 - etc, not 1 - 2 - 3 - etc 
        */
    }
    $output .= "</ul>";
    return $output;
}

$json_data = '{"ch":"79",
                "ipaddr":"172.16.11.100",
                "hostname":"TMC-MALL01",
                "posisi":"2",
                "monitorid":"17",
                "view_dimension":"2*2",
                "input_ipaddr":""
            }{
                "ch":"78",
                "ipaddr":"172.16.11.99",
                "hostname":"TMC-JSB01",
                "posisi":"1",
                "monitorid":"17",
                "view_dimension":"2*2",
                "input_ipaddr":""
            }{
                "ch":"80",
                "ipaddr":"172.16.11.101",
                "hostname":"TMC-MALL02",
                "posisi":"3",
                "monitorid":"17",
                "view_dimension":"2*2",
                "input_ipaddr":""
            }{
                "ch":"92",
                "ipaddr":"172.16.11.106",
                "hostname":"TMC-PALMA01",
                "posisi":"4",
                "monitorid":"17",
                "view_dimension":"2*2",
                "input_ipaddr":""
            }{
                "ch":"93",
                "ipaddr":"172.16.11.107",
                "hostname":"TMC-PALMA02",
                "posisi":"5",
                "monitorid":"17",
                "view_dimension":"2*2",
                "input_ipaddr":""
            }{
                "ch":"112",
                "ipaddr":"172.16.11.129",
                "hostname":"TMC-R.PRM01",
                "posisi":"6",
                "monitorid":"17",
                "view_dimension":"2*2",
                "input_ipaddr":""
            }{
                "ch":"113",
                "ipaddr":"172.16.11.130",
                "hostname":"TMC-R.PRM03",
                "posisi":"8",
                "monitorid":"17",
                "view_dimension":"2*2",
                "input_ipaddr":""
            }{
                "ch":"114",
                "ipaddr":"172.16.11.131",
                "hostname":"TMC-R.PRM02",
                "posisi":"7",
                "monitorid":"17",
                "view_dimension":"2*2",
                "input_ipaddr":""
            }{
                "ch":"115",
                "ipaddr":"172.16.11.132",
                "hostname":"TMC-R.PRM04",
                "posisi":"9",
                "monitorid":"17",
                "view_dimension":"2*2",
                "input_ipaddr":""
            }';

$render = renderLayout3x3($json_data);
print $render; ?>

output always :

Output : "2","1","3","4","5","6","7","8","9" Why : 2 - 1 - 3 - etc, not 1 - 2 - 3 - etc

this is other output : output iteration

Aucun commentaire:

Enregistrer un commentaire