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 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答