亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

fwrite() 兩次寫入一個 json 編碼的數組

fwrite() 兩次寫入一個 json 編碼的數組

PHP
LEATH 2022-07-09 10:46:02
我正在嘗試使用通過獲取請求收集的數據編寫一個 json 文件。json文件是一個二維的字符串數組,但是當數據寫入文件時,嵌套數組會被寫入兩次。<?php//used for parsing htmlinclude("simple_html_dom.php");//read the file $fp = fopen("j.json", "r");$t = fread($fp, filesize("j.json"));fclose($fp);$loaded = json_decode($t);//print the loaded arrayprint_r($loaded);//gathering the data$url = "https://www.soldionline.it/quotazioni/dettaglio/IT0003934657.html";$prezzo1 = file_get_html($url)->find("span[class=val] b", 0)->plaintext;$data = file_get_html($url)->find("span[class=ora] b", 0)->plaintext;$url = "https://www.soldionline.it/quotazioni/dettaglio/IT0003934657.html";$prezzo2 = file_get_html($url)->find("span[class=val] b", 0)->plaintext;$url = "https://www.soldionline.it/quotazioni/dettaglio/IT0003934657.html";$prezzo3 = file_get_html($url)->find("span[class=val] b", 0)->plaintext;//adding the new data to the arrayarray_push($loaded, array($prezzo1, $prezzo2, $prezzo3, $data));//the new json string is parsed and ready to be written$s = json_encode($loaded);//printing stuff to ensure the data is correctecho "<br>".$s.", type=".gettype($s)."<br>";print_r($loaded);//write the new json string to the same file$fp = fopen("j.json", "w");fwrite($fp, $s);fclose($fp);?>j.json 在腳本運行之前:[]腳本打印的內容:Array ( )[["128,54","128,54","128,54","30\/12"]], type=stringArray ( [0] => Array ( [0] => 128,54 [1] => 128,54 [2] => 128,54 [3] => 30/12 ) )腳本后的 j.json:[["128,54","128,54","128,54","30\/12"],["128,54","128,54","128,54","30\/12"]]我嘗試像這樣打開文件:$fp = fopen("j.json", "r+");然后我更改了腳本:$s = "\"".json_encode($loaded)."\"";echo "<br>".$s.", type=".gettype($s)."<br>";print_r($loaded);fwrite($fp, $s);fclose($fp);我發現也寫了一個空值:[]"[["128,54","128,54","128,54","30\/12"]]""null"
查看完整描述

1 回答

?
SMILET

TA貢獻1796條經驗 獲得超4個贊

瀏覽器在訪問 url 時發送兩個請求,一個對 php 文件的請求,另一個對/favicon.ico. 發送第二個請求以檢查站點是否具有圖標。這第二個請求導致腳本執行兩次。

查看完整回答
反對 回復 2022-07-09
  • 1 回答
  • 0 關注
  • 212 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號