[英]Using json_encode() in PHP and JSON.parse() in JavaScript to read/write from/to a file
我在PHP
使用json_encode()
將數組寫入文件,然后在客戶端在JavaScript
使用JSON.parse()
讀取json編碼的文件並將其作為數組傳遞給排序算法:
我的json_encode()輸出文件:[“ 1”,“ 96”,“ 32”,“ 33”,“ 4”,“ 48”,“ 74”,“ 19”,“ 23”,“ 43”,“ 8 “,” 8“,” 46“,” 36“,” 92“,” 81“,” 81“,” 64“,” 26“,” 96“,” 82“,” 85“,” 80“, “ 24”,“ 61”,“ 4”,“ 46”,“ 32”,“ 68”,“ 11”,“ 63”,“ 14”,“ 98”,“ 20”,“ 66”,“ 17” “,” 28“,” 58“,” 32“,” 16“,” 33“,” 47“,” 80“,” 94“,” 5“,” 68“,” 35“,” 28“, “ 24”,“ 85”,“ 38”,“ 12”,“ 79”,“ 57”,“ 6”,“ 47”,“ 18”,“ 15”,“ 34”,“ 18”,“ 91 “,” 63“,” 67“,” 73“,” 86“,” 16“,” 71“,” 29“,” 14“,” 79“,” 18“,” 10“,” 97“, “ 29”,“ 1”,“ 97”,“ 72”,“ 92”,“ 42”,“ 19”,“ 25”,“ 76”,“ 38”,“ 25”,“ 21”,“ 37 “]
使用JSON.parse()之后,我將數組返回到variabel
unsortedArray
,該數組如下:索引:0
值:1,96,32,33,4,48,74,19,23,43,8,8,46,36,92,81,81,64,26,96,82,85,80,24, 61,4,46,32,68,11,63,14,98,20,66,17,28,58,32,16,33,47,80,94,5,68,35,28,24, 85,38,12,79,57,6,47,18,15,34,18,91,63,67,73,86,16,71,29,14,79,18,10,97,29, 1,97,72,92,42,19,25,76,38,25,21,37
但是當我通過諸如Bublesort之類的排序算法(我知道它不是最好的)將其放入時,我得到一個奇怪的輸出:
[0] => Array
(
[0] => 1
[1] => 1
[2] => 10
[3] => 11
[4] => 12
[5] => 14
[6] => 14
[7] => 15
[8] => 16
[9] => 16
[10] => 17
[11] => 18
[12] => 18
[13] => 18
[14] => 19
[15] => 19
[16] => 20
[17] => 21
[18] => 23
[19] => 24
[20] => 24
[21] => 25
[22] => 25
[23] => 26
[24] => 28
[25] => 28
[26] => 29
[27] => 29
[28] => 32
[29] => 32
[30] => 32
[31] => 33
[32] => 33
[33] => 34
[34] => 35
[35] => 36
[36] => 37
[37] => 38
[38] => 38
[39] => 4
[40] => 4
[41] => 42
[42] => 43
[43] => 46
[44] => 46
[45] => 47
[46] => 47
[47] => 48
[48] => 5
[49] => 57
[50] => 58
[51] => 6
[52] => 61
[53] => 63
[54] => 63
[55] => 64
[56] => 66
[57] => 67
[58] => 68
[59] => 68
[60] => 71
[61] => 72
[62] => 73
[63] => 74
[64] => 76
[65] => 79
[66] => 79
[67] => 8
[68] => 8
[69] => 80
[70] => 80
[71] => 81
[72] => 81
[73] => 82
[74] => 85
[75] => 85
[76] => 86
[77] => 91
[78] => 92
[79] => 92
[80] => 94
[81] => 96
[82] => 96
[83] => 97
[84] => 97
[85] => 98
)
我用排序算法和類似bubleSort.apply(this, unsortedArray);
的輸入調用函數bubleSort.apply(this, unsortedArray);
。
但是,如上所示,它沒有完全排序,除了數字1(恰好在正確的位置),4、5、6和8之外的所有東西都被排序了,我不明白為什么會這樣。
更新:
$jobRetParam
具有來自另一頁的所有結果,它是使用POST
將JavaScript代碼通過以下代碼發送到網站的輸出:
$jobRetParam = $_REQUEST['results'];
$ allOutputVarPath變量在此處創建:
$allOutputVarPath = array();
$i = 0;
foreach ($allOutputVar as $key => $values) {
foreach ($values as $key => $value) {
if ($key == 4) {
$allOutputVarPath[] = array($value => $jobRetParam[$i]);
$i++;
}
}
}
$allOutputVar
來自數據庫, $key == 4
是$value
在下面循環的$path
變量中保存我正在使用的$path
。 $jobRetParam[$i]
在“ Update ”一詞下進行解釋,每個$value
都有自己的結果集。
我有很多代碼,但最后編碼到Json
時涉及到此:
foreach ($allOutputVarPath as $key => $values) {
foreach ($values as $path => $fileContent) {
$writeSuccess = wrtieToHDD($path, "w", $fileContent);
}
}
Hier是
$path => $fileContent
的示例,我在wrtieToHDD()
函數中將其用作Json
的輸入,在該函數中,我將$fileContent
編碼為Json
。
$path
:[... / sorted2.txt]
$fileContent
:數組([0] => 1 [1] => 1 [2] => 10 [3] => 11 [4] => 12 [5] => 14 [6] => 14 [7] => 15 [8] => 16 [9] => 16 [10] => 17 [11] => 18 [12] => 18 [13] => 18 [14] => 19 [15] => 19 [16] => 20 [17] => 21 [18] => 23 [19] => 24 [20] => 24 [21] => 25 [22] => 25 [23] => 26 [ 24] => 28 [25] => 28 [26] => 29 [27] => 29 [28] => 32 [29] => 32 [30] => 32 [31] => 33 [32] => 33 [33] => 34 [34] => 35 [35] => 36 [36] => 37 [37] => 38 [38] => 38 [39] => 4 [40] => 4 [41] => 42 [42] => 43 [43] => 46 [44] => 46 [45] => 47 [46] => 47 [47] => 48 [48] => 5 [ 49] => 57 [50] => 58 [51] => 6 [52] => 61 [53] => 63 [54] => 63 [55] => 64 [56] => 66 [57] => 67 [58] => 68 [59] => 68 [60] => 71 [61] => 72 [62] => 73 [63] => 74 [64] => 76 [65] => 79 [66] => 79 [67] => 8 [68] => 8 [69] => 80 [70] => 80 [71] => 81 [72] => 81 [73] => 82 [ 74] => 85 [75] => 85 [76] => 86 [77] => 91 [78] => 92 [79] => 92 [80] => 94 [81] => 96 [82] => 96 [83] => 97 [84] => 97 [85] => 98)
和wrtieToHDD()
函數:
function wrtieToHDD ($destination, $fopenMode, $content) {
$handle = fopen($destination, $fopenMode);
// check if $content is an array
if (gettype($content) == "array" or "object") {
// encode as json
$content = json_encode($content);
}
// write into file
$writeContent = FALSE;
if (flock($handle, LOCK_EX)) { // exclusiv lock
fwrite($handle, $content);
fflush($handle); // clear the output buffer bevor the lock free
flock($handle, LOCK_UN); // free lock
$writeContent = TRUE;
}
fclose($handle);
return $writeContent;
}
這應該照顧它。
更改:
$allOutputVarPath[] = array($value => $jobRetParam[$i]);
至:
$allOutputVarPath[] = array($value => (int)$jobRetParam[$i]);
在您的原始版本中,數字作為值的字符串表示形式返回到$_REQUEST['results']
中。 如果您曾經對其中一個字符串進行過算術運算,PHP會即時將字符串轉換為數字以正確處理它。
但是,在這種情況下,每個變量最終都只是轉換為JSON,而JSON轉換不知道它應該是數字,因此將其保留為字符串。
(int)
將字符串轉換為整數。 有關更多詳細信息,請參見此處: http : //php.net/manual/zh/language.types.type-juggling.php#language.types.typecasting
我認為您的問題出在$jobRetParam = $_REQUEST['results'];
。 $_REQUEST
通過urldecode()
傳遞,因此其所有成員都是字符串而不是數字。 當然,當您將字符串分配給$allOutputVarPath
,您將在json中獲取與排序混亂的字符串。
您可以像在$allOutputVarPath[] = array($value => intval($jobRetParam[$i]));
那樣使用intval()
$allOutputVarPath[] = array($value => intval($jobRetParam[$i]));
或在排序時將字符串轉換為javascript中的數字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.