简体   繁体   中英

how to add or insert record in json file

how can i add or insert record(in a form of array) in my json file using php or javascript?

i've tried this one but no luck:

$file = file_get_contents('Copy.json');
            $tempArray = json_decode($file);

            $arr = array('type' => 'Feature',
                        'id'=>$adsID,
                        'properties' => array(
                        'IMAGE'=>$ALogo,
                        'NAME'=>$name,
                        'TEL'=>$ATel,
                        'URL'=>$url,
                        'ADRESS1'=>$A_add,
                        'ADDRESS2'=>'add2',
                        'CITY'=>'Angeles City',
                        'ZIP'=>'2009'),
                            'geometry' => array(
                            'type' => 'Point',
                            'coordinates' => array ($ACoords1))
                        ) ;// features


            echo json_encode($arr);//$data[] = $_POST['data'];

            array_push($tempArray, $arr);
            $jsonData = json_encode($tempArray);
            file_put_contents('Copy.json', $jsonData);

array merge $file = file_get_contents('Copy.json'); $tempArray = json_decode($file);

        $arr = array('type' => 'Feature',
                    'id'=>$adsID,
                    'properties' => array(
                    'IMAGE'=>$ALogo,
                    'NAME'=>$name,
                    'TEL'=>$ATel,
                    'URL'=>$url,
                    'ADRESS1'=>$A_add,
                    'ADDRESS2'=>'add2',
                    'CITY'=>'Angeles City',
                    'ZIP'=>'2009'),
                        'geometry' => array(
                        'type' => 'Point',
                        'coordinates' => array ($ACoords1))
                    ) ;// features


        echo json_encode($arr);//$data[] = $_POST['data'];

        $merge = array_merge($tempArray, $arr);
        $jsonData = json_encode($merge);
        file_put_contents('Copy.json', $jsonData);

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