简体   繁体   中英

Decode a json string into an array

I am having trouble to decode a json string into an array but I am getting the following error.

ERROR: (Parsing error occurred while we processed your request.)

$value = '"latitude":"1.3704","longitude":"103.8471","city":"singapore"';

$jsondata = file_get_contents($value);
$db = json_decode($jsondata,true);  

for( $i = 0; $i < sizeof($db); $i++){
    $data = $db[$i];
    if( $data['city'] == $_POST['city'] ){
        if( isset($_POST['radius']) ){
            $distance = 6371000 * acos( cos( deg2rad($data['latitude']) ) * cos( deg2rad( $latitude ) ) * cos( deg2rad( $longitude ) - deg2rad($data['longitude']) ) + sin( deg2rad($data['latitude']) ) * sin( deg2rad( $latitude ) ) );
            if( $distance <= $radius ){
                $inradius = true;
            }
            else{
                $inradius = false;
            }
        }
        else{
            $inradius = true;
        }
        if( $data['price'] >= $minPrice && $data['price'] <= $maxPrice && $data['area'] >= $minArea && $data['area'] <= $maxArea && $inradius === true){
            if( empty( $_POST['slika'] ) ){
                $result[] = $data;
            }
            else{
                if( !empty( $data['images'] )){
                    $result[] = $data;
                }
            }
        }

This is not valid JSON:

"latitude":"1.3704","longitude":"103.8471","city":"singapore"

This is valid JSON:

{"latitude":"1.3704","longitude":"103.8471","city":"singapore"}
^                                                             ^

Best way is to use built-in function json_encode() :

$json = json_encode
(
    [ 'latitude' => '1.3704', 'longitude' => '103.8471', 'city' => 'singapore' ]
);

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