Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中将数组转换为对象数组_Php_Arrays_Json - Fatal编程技术网

在php中将数组转换为对象数组

在php中将数组转换为对象数组,php,arrays,json,Php,Arrays,Json,这可能很容易,但我正在努力完成 我在php中有以下数组,我想将其json_编码为一个对象数组。在php中实现这一点的最佳方法是什么 Array ( [6946] => Array ( [AssetGroupName] => Computer [AssetGroupID] => 14 ) [6945] => Array ( [AssetG

这可能很容易,但我正在努力完成

我在php中有以下数组,我想将其json_编码为一个对象数组。在php中实现这一点的最佳方法是什么

Array
(
    [6946] => Array
        (
            [AssetGroupName] => Computer
            [AssetGroupID] => 14
        )

    [6945] => Array
        (
            [AssetGroupName] => Laptop
            [AssetGroupID] => 15
        )
)
我正在尝试获取Json输出

{ 
   "data": [
      {
         "AssetGroupName": "Computer",
         "AssetGroupID": "12"
      },
      {
         "AssetGroupName": "Laptop",
         "AssetGroupID": "15"
      }
   ]
}

这相对简单,首先,您需要使用嵌套的
数据
子数组在PHP中创建适当的数组,然后只是为了去掉键(或者按照其他顺序由您决定)。使用不是从0开始并增加1的数字键时,它们仅被视为关联数组中的键

$array=[
“数据”=>[
6946 => [
'AssetGroupName'=>'Computer',
'AssetGroupID'=>14
],
6945 => [
'AssetGroupName'=>'Laptop',
“AssetGroupID”=>15
]
]
];
$array['data']=数组_值($array['data']);
echo json_编码($array);

json\u encode()
将关联数组编码为json对象。感谢您的解释。使用
array\u值
成功了。再次感谢。我敢打赌,OP不想手动重写数组数据来删除密钥。
> $array = [
>     ['AssetGroupName' => 'computer', 'AssetGroupID' => 14], ];
> 
> echo json_encode(['data' => $array]);