douxia3505 2017-01-25 11:38
浏览 81
已采纳

数组到没有索引的子

I have this code, basically take some lines and put in a array in groups of 4

Originally the data comes in this format:

line1
line2
line3
line4
line5
line6
line7
line8

and with this code i pass the lines to array:

$addresses = [];
foreach (preg_split("/((?
)|(
?))/", $nodes) as $line) {
    $temp[] = trim($line);
    if(count($temp) == 4){
        array_push($addresses,[
                    'ADDRESS' => $temp[0],
                    'STREET_NAME' => $temp[1],
                    'TOWN_NAME' => $temp[2],
                    'POST_CODE' => $temp[3]
                ]);
      $temp = [];
  }
}
var_dump(json_encode($addresses,JSON_PRETTY_PRINT));

But the result its not what i want... :

{
  "1": {
      "ADDRESS": "10",
      "STREET_NAME": "TRIQ ID-DAR TA` PULTU",
      "TOWN_NAME": "BIRZEBBUGA",
      "POST_CODE": "BBG 1810"
  },
  "2": {
      "ADDRESS": "12",
      "STREET_NAME": "TRIQ ID-DAR TA` PULTU",
      "TOWN_NAME": "BIRZEBBUGA",
      "POST_CODE": "BBG 1810"
  }
}

And i dont want the index...

i need something like this:

[
{
  "ADDRESS": "10",
  "STREET_NAME": "TRIQ ID-DAR TA` PULTU",
  "TOWN_NAME": "BIRZEBBUGA",
  "POST_CODE": "BBG 1810"
},
{
  "ADDRESS": "12",
  "STREET_NAME": "TRIQ ID-DAR TA` PULTU",
  "TOWN_NAME": "BIRZEBBUGA",
  "POST_CODE": "BBG 1810"
}
]

Any idea about the mistake???

  • 写回答

1条回答 默认 最新

  • doumindang2416 2017-01-25 11:55
    关注

    This will create what you are asking for

    $nodes = 'line1
    line2
    line3
    line4
    line5
    line6
    line7
    line8
    ';
    
    $addresses = [];
    foreach (preg_split("/((?
    )|(
    ?))/", $nodes) as $line) {
        $temp[] = trim($line);
        if(count($temp) == 4){
            $obj = new stdClass();
            $obj->ADDRESS =  $temp[0];
            $obj->STREET_NAME = $temp[1];
            $obj->TOWN_NAME = $temp[2];
            $obj->POST_CODE = $temp[3];
    
            $addresses[] = $obj;
            $temp = [];
        }
    }
    var_dump(json_encode($addresses,JSON_PRETTY_PRINT));
    

    Result:

    string(266) "[
        {
            "ADDRESS": "line1",
            "STREET_NAME": "line2",
            "TOWN_NAME": "line3",
            "POST_CODE": "line4"
        },
        {
            "ADDRESS": "line5",
            "STREET_NAME": "line6",
            "TOWN_NAME": "line7",
            "POST_CODE": "line8"
        }
    ]"
    

    But when you look at the data encoded and then decoded back to a PHP datatype, you will still get your objects indexed numerically, as an array has to have an index. This applies to PHP and the javascript equivalent when you get this data to javascript

    print_r(json_decode(json_encode($addresses)));
    

    Result

    Array
    (
        [0] => stdClass Object
            (
                [ADDRESS] => line1
                [STREET_NAME] => line2
                [TOWN_NAME] => line3
                [POST_CODE] => line4
            )
    
        [1] => stdClass Object
            (
                [ADDRESS] => line5
                [STREET_NAME] => line6
                [TOWN_NAME] => line7
                [POST_CODE] => line8
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法