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

4
  • 4
    This looks like JSON. See php.net/manual/en/function.json-decode.php Commented Jun 18, 2018 at 20:17
  • 1
    3v4l.org/PEDGK Commented Jun 18, 2018 at 20:19
  • 1
    This is json. You can use functions to decode json into array and then can manipulate and forumulate the array of your need accordingly. Commented Jun 18, 2018 at 20:24
  • Thanks everyone it solved my issue. And sorry, didn't know it was JSON ! Can one of you post it as an answer ? Commented Jun 18, 2018 at 20:38

2 Answers 2

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>";
Sign up to request clarification or add additional context in comments.

Comments

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);

8 Comments

Can you explain why are you casting it to an array afterwards?
cause it returns it as a stdClass object
he said he wanted an array
In the manual there is a good explanation of what the second argument of json_decode does.
Please see the second argument of json_decode, which should be used for returning an array instead of an object.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.