我有这个数组,我想把它编码成json ..
Array
(
[0] => Array
(
[0] => . Hour
[1] => bteam_pvp
[2] => crackpack
[3] => tppi
[4] => agrarian_plus
[5] => agrarian2
[6] => bteam_pve
[7] => agrarian
[8] => horizons
[9] => m_lobby
[10] => m_north
[11] => m_south
[12] => m_east
[13] => hub
)
[1] => Array
(
[0] => 1
[1] => 94
[2] => 96
[3] => 96
[4] => 98
[5] => 71
[6] => 49
[7] => 64
[8] => 84
[9] => 91
[10] => 70
[11] => 97
[12] => 83
[13] => 98
)
[2] => Array
(
[0] => 2
[1] => 95
[2] => 97
[3] => 95
[4] => 97
[5] => 70
[6] => 42
[7] => 57
[8] => 94
[9] => 97
[10] => 85
[11] => 98
[12] => 90
[13] => 99
)
[3] => Array
(
[0] => 3
[1] => 94
[2] => 96
[3] => 96
[4] => 97
[5] => 63
[6] => 37
[7] => 56
[8] => 96
[9] => 97
[10] => 96
[11] => 98
[12] => 93
[13] => 99
)
[4] => Array
(
[0] => 4
[1] => 94
[2] => 96
[3] => 98
[4] => 97
[5] => 65
[6] => 26
[7] => 60
[8] => 96
[9] => 97
[10] => 94
[11] => 98
[12] => 93
[13] => 97
)
[5] => Array
(
[0] => 5
[1] => 96
[2] => 97
[3] => 99
[4] => 96
[5] => 58
[6] => 63
[7] => 66
[8] => 87
[9] => 98
[10] => 96
[11] => 95
[12] => 94
[13] => 99
)
[6] => Array
(
[0] => 6
[1] => 97
[2] => 98
[3] => 98
[4] => 96
[5] => 45
[6] => 87
[7] => 63
[8] => 82
[9] => 97
[10] => 96
[11] => 98
[12] => 95
[13] => 99
)
[7] => Array
(
[0] => 7
[1] => 96
[2] => 98
[3] => 98
[4] => 97
[5] => 65
[6] => 80
[7] => 76
[8] => 56
[9] => 98
[10] => 93
[11] => 99
[12] => 96
[13] => 99
)
[8] => Array
(
[0] => 8
[1] => 98
[2] => 98
[3] => 99
[4] => 98
[5] => 71
[6] => 88
[7] => 83
[8] => 79
[9] => 99
[10] => 95
[11] => 99
[12] => 97
[13] => 99
)
[9] => Array
(
[0] => 9
[1] => 98
[2] => 98
[3] => 99
[4] => 98
[5] => 64
[6] => 95
[7] => 78
[8] => 85
[9] => 99
[10] => 95
[11] => 99
[12] => 97
[13] => 99
)
[10] => Array
(
[0] => 10
[1] => 98
[2] => 98
[3] => 99
[4] => 98
[5] => 75
[6] => 94
[7] => 77
[8] => 95
[9] => 99
[10] => 95
[11] => 98
[12] => 97
[13] => 99
)
[11] => Array
(
[0] => 11
[1] => 98
[2] => 98
[3] => 98
[4] => 98
[5] => 73
[6] => 94
[7] => 79
[8] => 98
[9] => 98
[10] => 88
[11] => 98
[12] => 97
[13] => 99
)
[12] => Array
(
[0] => 12
[1] => 98
[2] => 98
[3] => 98
[4] => 98
[5] => 75
[6] => 94
[7] => 84
[8] => 98
[9] => 98
[10] => 81
[11] => 98
[12] => 97
[13] => 99
)
[13] => Array
(
[0] => 13
[1] => 98
[2] => 95
[3] => 97
[4] => 98
[5] => 87
[6] => 95
[7] => 83
[8] => 98
[9] => 97
[10] => 80
[11] => 98
[12] => 97
[13] => 99
)
[14] => Array
(
[0] => 14
[1] => 96
[2] => 98
[3] => 96
[4] => 98
[5] => 85
[6] => 84
[7] => 80
[8] => 95
[9] => 98
[10] => 92
[11] => 98
[12] => 97
[13] => 99
)
[15] => Array
(
[0] => 15
[1] => 88
[2] => 96
[3] => 89
[4] => 98
[5] => 79
[6] => 60
[7] => 78
[8] => 97
[9] => 98
[10] => 72
[11] => 96
[12] => 94
[13] => 99
)
[16] => Array
(
[0] => 16
[1] => 88
[2] => 94
[3] => 93
[4] => 98
[5] => 69
[6] => 50
[7] => 72
[8] => 97
[9] => 97
[10] => 50
[11] => 94
[12] => 96
[13] => 99
)
[17] => Array
(
[0] => 17
[1] => 84
[2] => 95
[3] => 70
[4] => 95
[5] => 65
[6] => 39
[7] => 61
[8] => 97
[9] => 95
[10] => 40
[11] => 91
[12] => 96
[13] => 98
)
[18] => Array
(
[0] => 18
[1] => 77
[2] => 91
[3] => 83
[4] => 94
[5] => 69
[6] => 61
[7] => 59
[8] => 85
[9] => 90
[10] => 59
[11] => 76
[12] => 90
[13] => 96
)
[19] => Array
(
[0] => 19
[1] => 74
[2] => 96
[3] => 93
[4] => 95
[5] => 59
[6] => 31
[7] => 59
[8] => 77
[9] => 94
[10] => 56
[11] => 80
[12] => 85
[13] => 96
)
[20] => Array
(
[0] => 20
[1] => 87
[2] => 95
[3] => 75
[4] => 96
[5] => 62
[6] => 22
[7] => 45
[8] => 76
[9] => 93
[10] => 73
[11] => 87
[12] => 62
[13] => 96
)
[21] => Array
(
[0] => 21
[1] => 83
[2] => 92
[3] => 71
[4] => 98
[5] => 50
[6] => 37
[7] => 56
[8] => 83
[9] => 88
[10] => 97
[11] => 93
[12] => 77
[13] => 97
)
[22] => Array
(
[0] => 22
[1] => 84
[2] => 91
[3] => 87
[4] => 97
[5] => 50
[6] => 26
[7] => 67
[8] => 56
[9] => 86
[10] => 91
[11] => 82
[12] => 66
[13] => 96
)
[23] => Array
(
[0] => 23
[1] => 91
[2] => 86
[3] => 78
[4] => 98
[5] => 47
[6] => 31
[7] => 67
[8] => 81
[9] => 83
[10] => 66
[11] => 56
[12] => 55
[13] => 88
)
[24] => Array
(
[0] => 24
[1] => 91
[2] => 81
[3] => 64
[4] => 98
[5] => 72
[6] => 37
[8] => 63
[9] => 93
[10] => 59
[11] => 92
[12] => 67
[13] => 98
)
)
所以我用这个:
[<?php
foreach ($gPerformance as $row) {
echo json_encode($row) . ",";
}
?>]
不幸的是,这回应了这个:
[
[". Hour","bteam_pvp","crackpack","tppi","agrarian_plus","agrarian2","bteam_pve","agrarian","horizons","m_lobby","m_north","m_south","m_east","hub"],
[1,89,96,90,98,81,52,71,98,98,71,96,93,99],
[2,88,94,93,98,69,50,72,97,97,50,94,96,99],
[3,84,95,70,95,65,39,61,97,95,40,91,96,98],
[4,77,91,83,94,69,61,59,85,90,59,76,90,96],
[5,74,96,93,95,59,31,59,77,94,56,80,85,96],
[6,87,95,75,96,62,22,45,76,93,73,87,62,96],
[7,83,92,71,98,50,37,56,83,88,97,93,77,97],
[8,84,91,87,97,50,26,67,56,86,91,82,66,96],
[9,91,86,78,98,47,31,67,81,83,66,56,55,88],
[10,91,81,64,98,72,37,72,63,93,59,92,67,98],
[11,91,93,96,98,64,50,63,50,92,57,98,83,99],
[12,92,96,94,98,61,42,59,93,98,98,99,93,99],
[13,91,95,95,98,46,41,61,98,98,97,98,96,99],
[14,91,95,98,97,51,24,67,98,98,97,98,93,94],
[15,94,97,98,97,47,74,67,94,98,97,97,93,99],
[16,97,98,97,96,47,91,74,94,97,98,98,94,99],
[17,95,98,98,97,67,90,86,59,98,95,99,97,99],
[18,98,98,99,98,58,95,82,54,99,97,99,98,99],
[19,98,98,99,98,70,95,86,91,98,97,99,97,99],
[20,98,98,98,98,81,93,85,98,99,97,99,97,99],
[21,98,97,97,98,72,92,77,97,98,97,98,97,100],
[22,97,98,96,98,64,92,84,98,98,95,98,97,99],
[23,97,90,95,98,86,93,77,98,96,95,99,97,99],
{"0":24,"1":94,"2":96,"4":98,"5":82,"6":69,"8":97,"9":98,"10":96,"11":98,"12":97,"13":99}
]
你能解释一下为什么最后一个元素有{}
括号吗?即使我向它添加第25个数组,第24个也保留括号,我不知道为什么。
答案 0 :(得分:6)
为了编码为数组文字而不是对象,数组必须是非稀疏的索引数组。
在您的情况下,您的最后一个数组缺少[7]
,使其稀疏,因此编码为对象。
答案 1 :(得分:1)
如您所见,最后一行是具有指定键的数组:
{"0":24,"1":94,"2":96,"4":98,"5":82,"6":69,"8":97,"9":98,"10":96,"11":98,"12":97,"13":99}
为什么呢?因为你错过了键#34; 3&#34;,这是你输入的输入,但不是json输出。检查输入数组和最后一行的键。
如果您不想编辑输入数组,请执行以下操作:
[<?php
foreach ($gPerformance as $row) {
echo json_encode(array_values($row)) . ",";
}
?>]