简体   繁体   中英

how to format this json data using php

the link is here .please look at this bellow details and tell me how to get the value of each {} block

JSON DATA

 {
        "id": "1",
        "0": "1",
        "value": "Addalachchenai",
        "1": "Addalachchenai",
        "department_id": "6",
        "2": "6",
        "ordering": "1",
        "3": "1"
    }

    {
        "id": "2",
        "0": "2",
        "value": "Akkaraipattu ",
        "1": "Akkaraipattu ",
        "department_id": "6",
        "2": "6",
        "ordering": "2",
        "3": "2"
    }

    {
        "id": "3",
        "0": "3",
        "value": "Ampara ",
        "1": "Ampara ",
        "department_id": "6",
        "2": "6",
        "ordering": "3",
        "3": "3"
    }

PHP

<?php

try {
    $dbh = new PDO('mysql:host=localhost;dbname=$db', $user, $pass);
    foreach($dbh->query('SELECT * FROM `jos_jea_towns` LIMIT 0, 500 ') as $row) {
       echo '<pre>' . json_encode($row, JSON_PRETTY_PRINT).'</pre>';

    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

I want to get the VALUE of the joson data..

How to format above json data using php

Use

json_decode($jsonObject, true);

this function will convert your json data in to php array. But the code you are writing seems to be very inefficient, you should not run your queries in foreach() variables, you should use like this.

<?php

try {
    $dbh = new PDO('mysql:host=localhost;dbname=$db', $user, $pass);
    $result = $dbh->query('SELECT * FROM `jos_jea_towns` LIMIT 0, 500 ');
    foreach($result as $row) {
       echo '<pre>';
       print_r($row);
       echo '</pre>';
       //echo '<pre>' . json_encode($row, JSON_PRETTY_PRINT).'</pre>';

    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM