第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

fwrite() 兩次寫入一個(gè) json 編碼的數(shù)組

fwrite() 兩次寫入一個(gè) json 編碼的數(shù)組

PHP
LEATH 2022-07-09 10:46:02
我正在嘗試使用通過(guò)獲取請(qǐng)求收集的數(shù)據(jù)編寫一個(gè) json 文件。json文件是一個(gè)二維的字符串?dāng)?shù)組,但是當(dāng)數(shù)據(jù)寫入文件時(shí),嵌套數(shù)組會(huì)被寫入兩次。<?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 在腳本運(yùn)行之前:[]腳本打印的內(nèi)容: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);我發(fā)現(xiàn)也寫了一個(gè)空值:[]"[["128,54","128,54","128,54","30\/12"]]""null"
查看完整描述

1 回答

?
SMILET

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

瀏覽器在訪問(wèn) url 時(shí)發(fā)送兩個(gè)請(qǐng)求,一個(gè)對(duì) php 文件的請(qǐng)求,另一個(gè)對(duì)/favicon.ico. 發(fā)送第二個(gè)請(qǐng)求以檢查站點(diǎn)是否具有圖標(biāo)。這第二個(gè)請(qǐng)求導(dǎo)致腳本執(zhí)行兩次。

查看完整回答
反對(duì) 回復(fù) 2022-07-09
  • 1 回答
  • 0 關(guān)注
  • 209 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)