【发布时间】:2016-07-27 19:37:53
【问题描述】:
我在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() 后,我将数组返回到变量
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);。
但如上所示,它并没有完全排序,除了数字 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 变量中使用的路径的位置。 $jobRetParam[$i] 在“Update”字下解释,每个$value 都会得到他的一组结果。
我有很多代码,但最后在编码为Json 时会出现这种情况:
foreach ($allOutputVarPath as $key => $values) {
foreach ($values as $path => $fileContent) {
$writeSuccess = wrtieToHDD($path, "w", $fileContent);
}
}
Hier 是
$path => $fileContent的一个示例,我将其用作wrtieToHDD()函数中的输入,我将$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;
}
【问题讨论】:
-
按字母排序。如果要按数字排序,则需要加载带有数字的数组。在编码之前向我们展示您在 PHP 中构建数组的代码。
-
@JonathanM 我发布了更新
-
@JonathanM 你有没有看到任何导致奇怪输出的东西?
-
您是从位于
$path的文件中读取$fileContent吗?如果是这样,它会将其作为文本而不是数值拉入。请显示您为$allOutputVarPath赋值的位置。 -
@JonathanM 我正在编写
$fileContentJson 编码到位于$path变量中的文件,之后我使用 JSON.parse() 从文件中读取并创建一个我在不同的 JavaScript 函数(bubleSort())中用作输入的数组。$fileContent中的数组来自另一个页面,它是使用 POST 将 JavaScript 代码的输出发送到带有上述代码的站点。
标签: javascript php arrays json sorting