0

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

Bambou
  • 1,005
  • 10
  • 24

2 Answers2

1

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>";
Carlos
  • 240
  • 1
  • 5
  • 17
0

Here try this:

$json = '{"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}
                }';

$toarray = json_decode($json, true);
samezedi
  • 621
  • 5
  • 15