读取Json文件并提取值php mysql

时间:2017-02-02 09:33:36

标签: php mysql json parsing

我需要你的帮助,我想从存储在文件中的json数组中提取每个值。 例如:仅获取tel_fixe值:我的代码是=>

...
$result = mysqli_query($conn,"SELECT tel_fixe,tel_mobile FROM lead");
$table=array();
while($row=mysqli_fetch_assoc($result)){
    $table[] = $row;
}
file_put_contents("json_mysql/json".$date_test.".txt", json_encode($table));

$dossier_injection = 'inject_json/';
$dossier_backup = 'backup_json/';

$filenames = 'json'.$date_test.'.txt';
$filename = $dossier_injection.'json'.$date_test.'.txt';

$str = file_get_contents($filename);
$json = json_decode($str, true);

mysqli_query($conn,"insert into json_injection (nom_fichier, date_injection) values ('$filename','$date_test')");

foreach ($json as $key => $value) {
    foreach ($value as $key => $val) {
        if ($val <> 0 ) {
            mysqli_query($conn,"insert into result (tel_fixe,tel_mobile) value (????, ??????)");
        }
    }
}
rename($filename, 'backup_json/OK'.$filenames);

OUTPUT ==&GT;

输出

下面的代码
[{"tel_fixe":"xxxxxxxxxx","tel_mobile":"xxxxxxxxxx"},
{"tel_f‌​ixe":"xxxxxxxxxx","t‌​el_mobile":"xxxxxxxx‌​xx"},
{"tel_fixe":"xx‌​xxxxxxxx","tel_mobil‌​e":"xxxxxxxxxx"},
{"t‌​el_fixe":"xxxxxxxxxx‌​","tel_mobile":"xxxx‌​xxxxxx"},
{"tel_fixe"‌​:"xxxxxxxxxx","tel_m‌​obile":"xxxxxxxxxx"}‌​,
{"tel_fixe":"xxxxxx‌​xxxx","tel_mobile":"‌​xxxxxxxxxx"},.....]

谢谢:)(y)

0 个答案:

没有答案