简体   繁体   English

在PHP中使用json_encode()和JavaScript中的JSON.parse()来读取/写入文件

[英]Using json_encode() in PHP and JSON.parse() in JavaScript to read/write from/to a file

I'm using json_encode() in PHP to write an array into a file and after that I use JSON.parse() in JavaScript on the client side to read the json encoded file and pass it as an array to a sorting algorithm: 我在PHP使用json_encode()将数组写入文件,然后在客户端在JavaScript使用JSON.parse()读取json编码的文件并将其作为数组传递给排序算法:

  • My json_encode() output file: ["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_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 “]

  • After using JSON.parse() I get my array back in a variabel unsortedArray with: 使用JSON.parse()之后,我将数组返回到variabel unsortedArray ,该数组如下:

  • Index: 0 索引:0

  • Value: 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 值: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

But when I put it through a sorting algorithm like for example Bublesort (I know it's not the best one) I get a strange output: 但是当我通过诸如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
        )

I call the function with the sorting algorithm and the input like this bubleSort.apply(this, unsortedArray); 我用排序算法和类似bubleSort.apply(this, unsortedArray);的输入调用函数bubleSort.apply(this, unsortedArray); .

But as shown above it's not completely sorted, everything except the numbers 1 (just accidently in the right place), 4, 5, 6 and 8 is sorted, I don't understand why this is happening. 但是,如上所示,它没有完全排序,除了数字1(恰好在正确的位置),4、5、6和8之外的所有东西都被排序了,我不明白为什么会这样。

Update: 更新:

The $jobRetParam has all the results that comes from another page, it's an output of a JavaScript code being sent using POST to the site with this code: $jobRetParam具有来自另一页的所有结果,它是使用POST将JavaScript代码通过以下代码发送到网站的输出:

$jobRetParam = $_REQUEST['results'];

The $allOutputVarPath variable is created here: $ allOutputVarPath变量在此处创建:

$allOutputVarPath = array();
    $i = 0;
    foreach ($allOutputVar as $key => $values) {
        foreach ($values as $key => $value) {
            if ($key == 4) {
                $allOutputVarPath[] = array($value => $jobRetParam[$i]);
                $i++;
            }
        }
    }

The $allOutputVar comes from a Database the $key == 4 is where the $value holds the path that I'm using after that in the $path variable in the loop below. $allOutputVar来自数据库, $key == 4$value在下面循环的$path变量中保存我正在使用的$path The $jobRetParam[$i] is explained just under the " Update " word, each $value gets his set of results. $jobRetParam[$i]在“ Update ”一词下进行解释,每个$value都有自己的结果集。

I have a lot of code but at the end it comes to this when encoding to Json : 我有很多代码,但最后编码到Json时涉及到此:

foreach ($allOutputVarPath as $key => $values) {
        foreach ($values as $path => $fileContent) {
            $writeSuccess = wrtieToHDD($path, "w", $fileContent);
        }
}

Hier is an example of a $path => $fileContent that I use as an input in the wrtieToHDD() function where I encode the $fileContent as Json . Hier是$path => $fileContent的示例,我在wrtieToHDD()函数中将其用作Json的输入,在该函数中,我将$fileContent编码为Json

  • $path : [.../sorted2.txt] $path :[... / sorted2.txt]

  • $fileContent : 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 ) $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)

And the wrtieToHDD() function: 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;
}

This should take care of it. 这应该照顾它。

Change: 更改:

$allOutputVarPath[] = array($value => $jobRetParam[$i]);

To: 至:

$allOutputVarPath[] = array($value => (int)$jobRetParam[$i]);

In your original version, the numbers are coming back into $_REQUEST['results'] as string representations of the values. 在您的原始版本中,数字作为值的字符串表示形式返回到$_REQUEST['results']中。 If you were ever to do an arithmetic operation on one of the strings, PHP would convert the string to a number on the fly to deal properly with it. 如果您曾经对其中一个字符串进行过算术运算,PHP会即时将字符串转换为数字以正确处理它。

However, in this case, each is just eventually converted to JSON, and the JSON conversion doesn't know that it should be numeric, so it leaves it as a string. 但是,在这种情况下,每个变量最终都只是转换为JSON,而JSON转换不知道它应该是数字,因此将其保留为字符串。

The (int) casts the string to an integer. (int) 字符串转换为整数。 See here for more detail: http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting 有关更多详细信息,请参见此处: http : //php.net/manual/zh/language.types.type-juggling.php#language.types.typecasting

I think your problem is with the $jobRetParam = $_REQUEST['results']; 我认为您的问题出在$jobRetParam = $_REQUEST['results']; . The $_REQUEST is passed through urldecode() so all its members are strings instead of numbers. $_REQUEST通过urldecode()传递,因此其所有成员都是字符串而不是数字。 When you assign strings to $allOutputVarPath of course you're going to get string in your json that messes with your sorting. 当然,当您将字符串分配给$allOutputVarPath ,您将在json中获取与排序混乱的字符串。

You can either use intval() as in $allOutputVarPath[] = array($value => intval($jobRetParam[$i])); 您可以像在$allOutputVarPath[] = array($value => intval($jobRetParam[$i]));那样使用intval() $allOutputVarPath[] = array($value => intval($jobRetParam[$i])); or convert string to number in javascript when sorting. 或在排序时将字符串转换为javascript中的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM