简体   繁体   中英

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:

  • 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"]

  • After using JSON.parse() I get my array back in a variabel unsortedArray with:

  • Index: 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

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:

[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); .

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.

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 = $_REQUEST['results'];

The $allOutputVarPath variable is created here:

$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. The $jobRetParam[$i] is explained just under the " Update " word, each $value gets his set of results.

I have a lot of code but at the end it comes to this when encoding to 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 .

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

And the wrtieToHDD() function:

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. 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.

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.

The (int) casts the string to an integer. See here for more detail: http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting

I think your problem is with the $jobRetParam = $_REQUEST['results']; . The $_REQUEST is passed through urldecode() so all its members are strings instead of numbers. When you assign strings to $allOutputVarPath of course you're going to get string in your json that messes with your sorting.

You can either use intval() as in $allOutputVarPath[] = array($value => intval($jobRetParam[$i])); or convert string to number in javascript when sorting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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