I have an array which prints like this
我有一个像这样打印的数组。
Array ( [0] => 1691864 [1] => 7944458 [2] => 9274078 [3] => 1062072 [4] => 8625335 [5] => 8255371 [6] => 5476104 [7] => 6145446 [8] => 7525604 [9] => 5947143 )
If I json_encode($thearray)
I get something like this
如果我json_encode($thearray)会得到这样的东西
[1691864,7944458,9274078,1062072,8625335,8255371,5476104,6145446,7525604,5947143]
Why the name is not encoded (e.g 0, 1 , 2 , 3 etc) ? and how should I do to make it appear in the json code? the full code is below
为什么名称没有被编码(e)。g 0 1 2 3等等)?如何让它出现在json代码中呢?完整的代码在下面
$ie = 0;
while($ie 10)
{
$genid = rand(1000000,9999999);
$temp[$ie] = $genid ;
$ie++;
}
print_r($temp);
$temp_json = json_encode($temp);
print_r($temp_json);
4 个解决方案
#1
38
You can force that json_encode
uses an object although you’re passing an array with numeric keys by setting the JSON_FORCE_OBJECT option:
您可以强制json_encode使用对象,尽管您正在通过设置JSON_FORCE_OBJECT选项传递带有数字键的数组:
json_encode($thearray, JSON_FORCE_OBJECT)
Then the returned value will be a JSON object with numeric keys:
然后返回的值将是一个带有数字键的JSON对象:
{"0":1691864,"1":7944458,"2":9274078,"3":1062072,"4":8625335,"5":8255371,"6":5476104,"7":6145446,"8":7525604,"9":5947143}
But you should only do this if an object is really required.
但是,您应该只在对象确实是必需的时才这样做。
#2
5
Use this instead:
取代它可使用:
json_encode((object)$temp)
This converts the array into object, which when JSON-encoded, will display the keys.
这将数组转换为对象,当json编码时,对象将显示键。
If you are storing a sequence of data, not a mapping from number to another number, you really should use array.
如果你存储的是一个数据序列,而不是从数字到另一个数字的映射,你真的应该使用数组。
#3
0
Because those are just the indices of the array. If you want to add some kind of name to each element then you need to use an associative array.
因为这些只是数组的下标。如果要向每个元素添加某种名称,则需要使用关联数组。
When you decode that JSON array though it will come back out to 0, 1, 2, 3 etc.
当你解码JSON数组时它会回到0 1 2 3等等。
#4
0
This is defined behaviour. The array you show is a non-associative, normally indexed array. Its indexes are implicitly numeric.
这是定义的行为。您显示的数组是一个非关联的、通常索引的数组。它的索引是隐式的数字。
If you decode the array in PHP or JavaScript, you will be able to access the elements using the index:
如果用PHP或JavaScript解码数组,就可以使用索引访问元素:
$temp_array = json_decode($temp_json);
echo $temp_array[2]; // 9274078
#1
38
You can force that json_encode
uses an object although you’re passing an array with numeric keys by setting the JSON_FORCE_OBJECT option:
您可以强制json_encode使用对象,尽管您正在通过设置JSON_FORCE_OBJECT选项传递带有数字键的数组:
json_encode($thearray, JSON_FORCE_OBJECT)
Then the returned value will be a JSON object with numeric keys:
然后返回的值将是一个带有数字键的JSON对象:
{"0":1691864,"1":7944458,"2":9274078,"3":1062072,"4":8625335,"5":8255371,"6":5476104,"7":6145446,"8":7525604,"9":5947143}
But you should only do this if an object is really required.
但是,您应该只在对象确实是必需的时才这样做。
#2
5
Use this instead:
取代它可使用:
json_encode((object)$temp)
This converts the array into object, which when JSON-encoded, will display the keys.
这将数组转换为对象,当json编码时,对象将显示键。
If you are storing a sequence of data, not a mapping from number to another number, you really should use array.
如果你存储的是一个数据序列,而不是从数字到另一个数字的映射,你真的应该使用数组。
#3
0
Because those are just the indices of the array. If you want to add some kind of name to each element then you need to use an associative array.
因为这些只是数组的下标。如果要向每个元素添加某种名称,则需要使用关联数组。
When you decode that JSON array though it will come back out to 0, 1, 2, 3 etc.
当你解码JSON数组时它会回到0 1 2 3等等。
#4
0
This is defined behaviour. The array you show is a non-associative, normally indexed array. Its indexes are implicitly numeric.
这是定义的行为。您显示的数组是一个非关联的、通常索引的数组。它的索引是隐式的数字。
If you decode the array in PHP or JavaScript, you will be able to access the elements using the index:
如果用PHP或JavaScript解码数组,就可以使用索引访问元素:
$temp_array = json_decode($temp_json);
echo $temp_array[2]; // 9274078