PHP将数组转换为JSON问题?

PHP将数组转换为JSON问题?,php,arrays,json,Php,Arrays,Json,当我打印_r$arr时,类似于Array[0]=>Hello[1]=>world;是输出 我尝试使用下面的代码转换为JSON字符串 $result['result'] = $arr; json_encode($result); 这将产生以下JSON字符串: {"result" : { "0" : "hello" , "1" : "world"}} 预期结果如下: { "result" : ["hello" , "world"]} 我可以做些什么来获得所需的输出 $result['resul

当我打印_r$arr时,类似于Array[0]=>Hello[1]=>world;是输出

我尝试使用下面的代码转换为JSON字符串

$result['result'] = $arr;
json_encode($result);
这将产生以下JSON字符串:

{"result" : { "0" : "hello" , "1" : "world"}}
预期结果如下:

{ "result" : ["hello" , "world"]}
我可以做些什么来获得所需的输出

$result['result'] = array_values($arr);
json_encode($result);
仅使用这些值


仅使用值。

非常简单,请使用下面给出的代码

$arr = array("0"=>'hello',"1"=>'world');
$result['result'] = array_values($arr);
echo json_encode($result);
谢谢
Amit非常简单,请使用下面给出的代码

$arr = array("0"=>'hello',"1"=>'world');
$result['result'] = array_values($arr);
echo json_encode($result);
谢谢 Amit需要这样做:

$result['result'] = array_values($arr);
需要这样做:

$result['result'] = array_values($arr);

将数组值放入数组的结果键。然后它将打印所需的结果

$arr = array("0"=>'one',"1"=>'two');
$result['result'] = array_values($arr);
echo json_encode($result);

在线演示

将数组值放入数组的结果键。然后它将打印所需的结果

$arr = array("0"=>'one',"1"=>'two');
$result['result'] = array_values($arr);
echo json_encode($result);

在线演示

这真的是你的代码吗?阵列来自哪里?第二个结果看起来像您将得到的结果。我希望格式像{result:[hello,world]}。。。但我没有得到。Yupe它是我的codecheck此链接都显示相同的输出print\r$arr输出看起来像数组[0]=>Hello[1]=>world;是的,有$arr=arrayHello,world;你会得到你想要的结果,所以显示你的$arr;这真的是你的代码吗?阵列来自哪里?第二个结果看起来像您将得到的结果。我希望格式像{result:[hello,world]}。。。但我没有得到。Yupe它是我的codecheck此链接都显示相同的输出print\r$arr输出看起来像数组[0]=>Hello[1]=>world;是的,有$arr=arrayHello,world;你会得到你想要的结果,所以显示你的$arr;数组[0]=>你好[1]=>世界;->如果这真的是他得到的输出,那么$arr不是一个对象,而是一个数组;好吧,这是我的错误:数组[0]=>Hello[1]=>world;->如果这真的是他得到的输出,那么$arr不是一个对象,而是一个数组;好吧,这是我的错: