dongshen4129 2015-09-01 08:29
浏览 35
已采纳

拆分/子串控阵列 - PHP

I've been smashing my head the last few days, because I haven't been able to create a solution for my problem.

I have an array that looks like this:

[0] => {"count": 1038, "previous": null, "results": [{"data_source": "f31904c94a72490e8fa5750399a14e44", "uuid": "acbb6a8790604ac9916772f51729e69a", "reference": "2015-08-28-xxx@xxx.xx", "value": "0.0000", "member": "a7cab5fa045d44beb72da3ea26e6a49c", "HPSalesBudget": "1.00", "team": null, "date": "2015-08-28T00:00:00"}
[1] => {"data_source": "f31904c94a72490e8fa5750399a14e44", "uuid": "5ba77c689aba48f8ab845c9578b96d1d", "reference": "2015-08-28-xxxx@xxx.xx", "value": "0.0000", "member": "477039a270d841aaa87a5578ee034d60", "HPSalesBudget": "1.00", "team": null, "date": "2015-08-28T00:00:00"}

What I want is to remove the first part of the array

[0] => {"count": 1038, "previous": null, "results": 

And make and array that looks like this:

Array
(
[0] => Array
(
[data_source] => f31904c94a72490e8fa5750399a14e44
[uuid] => acbb6a8790604ac9916772f51729e69a
[reference] => 2015-08-28-xxx@xxx.xx
[value] => 0.0000
[member] => a7cab5fa045d44beb72da3ea26e6a49c
[HPSalesBudget] => 1.00
[team] => null
[date] => 2015-08-28T00:00:00
)

[1] => Array
(
[data_source] => f31904c94a72490e8fa5750399a14e44
[uuid] => acbb6a8790604ac9916772f51729e69a
[reference] => 2015-08-28-xxx@xxx.xx
[value] => 0.0000
[member] => a7cab5fa045d44beb72da3ea26e6a49c
[HPSalesBudget] => 1.00
[team] => null
[date] => 2015-08-28T00:00:00
)

The only reason why my array is looking like this is because that's the format I get the data in.

I have tried googling the problem but without any luck.

Does anybody have an solution for my problem?

  • 写回答

3条回答 默认 最新

  • duanfengtuo6012 2015-09-01 08:34
    关注

    Split your data into lines with something like explode(" ", $data);

    Then process the data line by line, strip the first 7 characters of each line, so that your data is only this:

    {"count": 1038, "previous": null, "results": [{"data_source": "f31904c94a72490e8fa5750399a14e44", "uuid": "acbb6a8790604ac9916772f51729e69a", "reference": "2015-08-28-xxx@xxx.xx", "value": "0.0000", "member": "a7cab5fa045d44beb72da3ea26e6a49c", "HPSalesBudget": "1.00", "team": null, "date": "2015-08-28T00:00:00"}
    

    Then use json_decode on each line.

    From there, it's easy to get the data in the format you need:

    $result = array();
    $result[] = $json['results'];
    

    I guess the code should look like this:

    $result = array();
    $lines = explode("
    ", $data);
    foreach ($lines as $line) {
        $line = substr($line, 7);
        $json = json_decode($line, true);
        $result[] = $json['results'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作