Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.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 - Fatal编程技术网

Php 将数组集合转换为对象

Php 将数组集合转换为对象,php,Php,我目前正在推送数组数据。如何像这样返回对象: "data": [ { "apartment": { { "id": xxx, "show": "xxx, }, { "id": xxx, "show": "xxx", }, "data": [

我目前正在推送数组数据。如何像这样返回对象:

"data": [
    {
        "apartment": {
            {
                "id": xxx,
                "show": "xxx,
            },
            {
                "id": xxx,
                "show": "xxx",
            },
 "data": [
    {
        "apartment": {
            "0": {
                "id": xxx,
                "show": "xxx,
            },
            "1" : {
                "id": xxx,
                "show": "xxx",
            },
data[1] = {"id" => 1}
不是这样的:

"data": [
    {
        "apartment": {
            {
                "id": xxx,
                "show": "xxx,
            },
            {
                "id": xxx,
                "show": "xxx",
            },
 "data": [
    {
        "apartment": {
            "0": {
                "id": xxx,
                "show": "xxx,
            },
            "1" : {
                "id": xxx,
                "show": "xxx",
            },
data[1] = {"id" => 1}
此代码:

data[] = array("id" => 1)
将向数据数组中添加命名数组

data = [ {"id" => 1} ]
如果希望数据是命名数组,则应按如下方式添加
“id”=>1

"data": [
    {
        "apartment": {
            {
                "id": xxx,
                "show": "xxx,
            },
            {
                "id": xxx,
                "show": "xxx",
            },
 "data": [
    {
        "apartment": {
            "0": {
                "id": xxx,
                "show": "xxx,
            },
            "1" : {
                "id": xxx,
                "show": "xxx",
            },
data[1] = {"id" => 1}
这将导致:

data = { 1 => {"id" => 1} }
此代码:

data[] = array("id" => 1)
将向数据数组中添加命名数组

data = [ {"id" => 1} ]
如果希望数据是命名数组,则应按如下方式添加
“id”=>1

"data": [
    {
        "apartment": {
            {
                "id": xxx,
                "show": "xxx,
            },
            {
                "id": xxx,
                "show": "xxx",
            },
 "data": [
    {
        "apartment": {
            "0": {
                "id": xxx,
                "show": "xxx,
            },
            "1" : {
                "id": xxx,
                "show": "xxx",
            },
data[1] = {"id" => 1}
这将导致:

data = { 1 => {"id" => 1} }

如果它们在同一个序列中会有什么区别?是的,我尝试将其转换为(对象),但索引仍然显示为这样,例如,它返回的数据没有索引。我觉得您的问题中缺少很多上下文。你想做什么?我想从列表中删除索引“0”。所以数据看起来像{“id”:xxx,“show”:“xxx,},{“id”:xxx,“show”:“xxx”}如果它们在相同的序列中,会有什么区别?是的,我尝试将其转换为(对象)但是索引仍然显示为这样,例如,它返回的数据没有索引。我觉得你的问题中缺少了很多上下文。你想实现什么?我试图从列表中删除索引“0”。因此数据看起来像{“id”:xxx,“show”:“xxx,},{“id”:xxx,“show”:“xxx”}是的,但是当我使用数据[]=array('id'=>1)时,我该怎么做呢;是的,但是当我使用data[]=array('id'=>1)时,我怎么做呢;