使用 json_encode() 将 PHP 数组转换为 JSON 数组;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11722059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Array to JSON Array using json_encode();
提问by Ryan Brodie
I've encoded an Array I've made using the inbuilt json_encode();function. I need it in the format of an Array of Arrays like so:
我已经使用内置json_encode();函数对我制作的数组进行了编码。我需要它的数组格式,如下所示:
[["Afghanistan",32,12],["Albania",32,12]]
However, it is returning as:
但是,它返回为:
["2":["Afghanistan",32,12],"4":["Albania",32,12]]
How can I remove these row numbers without using any Regex trickery?
如何在不使用任何正则表达式技巧的情况下删除这些行号?
回答by Michael Berkowski
If the array keys in your PHP array are not consecutive numbers, json_encode()mustmake the other construct an object since JavaScript arrays are always consecutively numerically indexed.
如果您的 PHP 数组中的数组键不是连续的数字,则json_encode()必须让另一个构造一个对象,因为 JavaScript 数组总是连续数字索引。
Use array_values()on the outer structure in PHP to discard the original array keys and replace them with zero-based consecutive numbering:
array_values()在 PHP 中的外部结构上使用以丢弃原始数组键并用从零开始的连续编号替换它们:
Example:
例子:
// Non-consecutive 3number keys are OK for PHP
// but not for a JavaScript array
$array = array(
2 => array("Afghanistan", 32, 13),
4 => array("Albania", 32, 12)
);
// array_values() removes the original keys and replaces
// with plain consecutive numbers
$out = array_values($array);
json_encode($out);
// [["Afghanistan", 32, 13], ["Albania", 32, 12]]
回答by Dinesh Gopal Chand
json_encode()function will help you to encode arrayto JSONin php.
json_encode()函数将帮助您在 php中将数组编码为JSON。
if you will use just json_encodefunction directly without any specific option, it will return an array. Like mention above question
如果您将直接使用json_encode函数而不使用任何特定选项,它将返回一个数组。就像上面提到的问题
$array = array(
2 => array("Afghanistan",32,13),
4 => array("Albania",32,12)
);
$out = array_values($array);
json_encode($out);
// [["Afghanistan",32,13],["Albania",32,12]]
Since you are trying to convert Array to JSON, Then I would suggest to use JSON_FORCE_OBJECTas additional option(parameters) in json_encode, Like below
既然您正在尝试将 Array 转换为 JSON,那么我建议在json_encode 中使用JSON_FORCE_OBJECT作为附加选项(参数),如下所示
<?php
$array=['apple','orange','banana','strawberry'];
echo json_encode($array, JSON_FORCE_OBJECT);
// {"0":"apple","1":"orange","2":"banana","3":"strawberry"}
?>
回答by Inc33
I want to add to Michael Berkowski's answer that this can also happen if the array's order is reversed, in which case it's a bit trickier to observe the issue, because in the json object, the order will be ordered ascending.
我想补充一下 Michael Berkowski 的回答,如果数组的顺序颠倒,也会发生这种情况,在这种情况下,观察这个问题有点棘手,因为在 json 对象中,顺序将按升序排列。
For example:
例如:
[
3 => 'a',
2 => 'b',
1 => 'c',
0 => 'd'
]
Will return:
将返回:
{
0: 'd',
1: 'c',
2: 'b',
3: 'a'
}
So the solution in this case, is to use array_reversebefore encoding it to json
所以在这种情况下的解决方案是array_reverse在将其编码为 json 之前使用
回答by L VEERAPANDI BCA
A common use of JSON is to read data from a web server, and display the data in a web page.
JSON 的一个常见用途是从 Web 服务器读取数据,并将数据显示在网页中。
This chapter will teach you how to exchange JSON data between the client and a PHP server.
本章将教你如何在客户端和 PHP 服务器之间交换 JSON 数据。
PHP has some built-in functions to handle JSON.
PHP 有一些内置函数来处理 JSON。
Objects in PHP can be converted into JSON by using the PHP function json_encode():
PHP 中的对象可以使用 PHP 函数 json_encode() 转换为 JSON:
<?php
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";
$myJSON = json_encode($myObj);
echo $myJSON;
?>
回答by Ray
If you don't specify indexes on your initial array, you get the regular numric ones. Arrays must have some form of unique index
如果您没有在初始数组上指定索引,则会得到常规的数字索引。数组必须有某种形式的唯一索引

