dqyat62284 2018-06-18 20:15
浏览 46
已采纳

将字符串转换为数组的数组[duplicate]

This question already has an answer here:

I need your help today to convert a string (I'll name it $data) like this one :

{"hours":{"2018-06-10 11:00":2,"2018-06-12 07:00":5,"2018-06-12 08:00":4,"2018-06-12 09:00":2,"2018-06-13 09:00":1,"2018-06-13 13:00":1,"2018-06-13 23:00":1,"2018-06-15 13:00":1,"2018-06-15 14:00":1,"2018-06-15 15:00":2,"2018-06-18 06:00":5,"2018-06-18 07:00":9,"2018-06-18 08:00":1,"2018-06-18 09:00":1,"2018-06-18 12:00":2,"2018-06-18 13:00":13},"days":{"2018-06-10 00:00":2,"2018-06-12 00:00":11,"2018-06-13 00:00":3,"2018-06-15 00:00":4,"2018-06-18 00:00":29,"2018-06-18 07:00":2},"weeks":{"2018-06-10 00:00":20,"2018-06-17 00:00":29,"2018-06-18 07:00":2}}

to something like this :

  • hours (Array)
    • 2018-06-10 11:00 (key) : 2 (value as int)
    • 2018-06-12 7:00 (key) : 5 (value as int)
    • etc...
  • days (Array)
    • 2018-06-10 00:00 (key) : 2 (value as int)
    • etc...
  • weeks (Array)
    • 2018-06-10 00:00 (key) : 20 (value as int)
    • etc...

I know there's some PHP functions like str_split or explode but I don't really know how to do. I've tried to make something like :

explode('{', $data);

or things like that but I don't really know where to start, if I explode with the ":", it will take the ":" in the date too.

If I forgot to put something, just tell me I'll edit the question.

Thanks in advance

</div>
  • 写回答

2条回答 默认 最新

  • dqjgf0982 2018-06-18 20:41
    关注

    Or you can to try this:

    $data = '{"hours":{"2018-06-10 11:00":2,"2018-06-12 07:00":5,"2018-06-12 08:00":4,"2018-06-12 09:00":2,"2018-06-13 09:00":1,"2018-06-13 13:00":1,"2018-06-13 23:00":1,"2018-06-15 13:00":1,"2018-06-15 14:00":1,"2018-06-15 15:00":2,"2018-06-18 06:00":5,"2018-06-18 07:00":9,"2018-06-18 08:00":1,"2018-06-18 09:00":1,"2018-06-18 12:00":2,"2018-06-18 13:00":13},"days":{"2018-06-10 00:00":2,"2018-06-12 00:00":11,"2018-06-13 00:00":3,"2018-06-15 00:00":4,"2018-06-18 00:00":29,"2018-06-18 07:00":2},"weeks":{"2018-06-10 00:00":20,"2018-06-17 00:00":29,"2018-06-18 07:00":2}}
    ';
    
    $dados =  json_decode( $data, true );
    echo "<pre>";
    print_r($dados);
    echo "</pre>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘