0

Input string:

$times = '{endTime:"2017-03-29T17:15:00.000+11:00",startTime:"2017-03-29T17:00:00.000+11:00"},{endTime:"2017-03-31T17:15:00.000+11:00",startTime:"2017-03-31T17:00:00.000+11:00"},{endTime:"2017-04-01T12:15:00.000+11:00",startTime:"2017-04-01T12:00:00.000+11:00"}';

And I'm trying to convert it into an array that should look like this:

Array
(
    [0] => Array
        (
            endTime => "2017-03-29T17:15:00.000+11:00"
            startTime => "2017-03-29T17:00:00.000+11:00"
        )

    [1] => Array
        (
            endTime => "2017-03-31T17:15:00.000+11:00"
            startTime => "2017-03-31T17:00:00.000+11:00"
        )

    [2] => Array
        (
            endTime => "2017-04-01T12:15:00.000+11:00"
            startTime => "2017-04-01T12:00:00.000+11:00"
        )

)

I've tried exploding, combining and all sorts but my code is so messy that I'm sure there must be a better and cleaner way?

This is MY cleanest starting point, but even this not clean, yes?

$times = '{endTime:"2017-03-29T17:15:00.000+11:00",startTime:"2017-03-29T17:00:00.000+11:00"},{endTime:"2017-03-31T17:15:00.000+11:00",startTime:"2017-03-31T17:00:00.000+11:00"},{endTime:"2017-04-01T12:15:00.000+11:00",startTime:"2017-04-01T12:00:00.000+11:00"}';
$timesarr = explode("},{", $times);

foreach ($timesarr as $i => $item) {
    $timesarr[$i] = str_replace("{", "", $item);
    $timesarr[$i] = str_replace("}", "", $timesarr[$i]);

    $timesarr[$i] = explode(",", $timesarr[$i]);
}


echo '<pre>'; print_r($timesarr); echo '</pre>';
2
  • you tried json_decode() Commented Mar 28, 2017 at 7:41
  • i think the string is not a valid json, convert it to valid json first and then decode this Commented Mar 28, 2017 at 7:43

4 Answers 4

3

Improvements done:

  1. Appended square braces at starting and end.

  2. Replaced words with [a-zA-Z]+ with "[a-zA-Z]+" making it a valid json than json_decode

PHP code demo

<?php

$times = '{endTime:"2017-03-29T17:15:00.000+11:00",startTime:"2017-03-29T17:00:00.000+11:00"},{endTime:"2017-03-31T17:15:00.000+11:00",startTime:"2017-03-31T17:00:00.000+11:00"},{endTime:"2017-04-01T12:15:00.000+11:00",startTime:"2017-04-01T12:00:00.000+11:00"}';
$times=$times."]";
$times="[".$times;
$jsonString=preg_replace("/([a-zA-Z]+\s*)\:/", '"$1":', $times);
print_r(json_decode($jsonString,true));
Sign up to request clarification or add additional context in comments.

1 Comment

THAT is beautiful!
1

This code will replace 'endTime' with '"endTime"' and same for startTime. I don't recommend you doing it this way, but it will work for you in this situation:

$times='{"endTime":"2017-03-29T17:15:00.000+11:00","startTime":"2017-03-29T17:00:00.000+11:00"},{"endTime":"2017-03-31T17:15:00.000+11:00","startTime":"2017-03-31T17:00:00.000+11:00"},{"endTime":"2017-04-01T12:15:00.000+11:00","startTime":"2017-04-01T12:00:00.000+11:00"}';
$times=str_replace("endTime",'"endTime"',$times);
$times=str_replace("startTime",'"startTime"',$times);
$times="[$times]";
echo "<h2><pre>";
print_r(json_decode($times,true));

This will output this:

Array
(
    [0] => Array
        (
            [endTime] => 2017-03-29T17:15:00.000+11:00
            [startTime] => 2017-03-29T17:00:00.000+11:00
        )

    [1] => Array
        (
            [endTime] => 2017-03-31T17:15:00.000+11:00
            [startTime] => 2017-03-31T17:00:00.000+11:00
        )

    [2] => Array
        (
            [endTime] => 2017-04-01T12:15:00.000+11:00
            [startTime] => 2017-04-01T12:00:00.000+11:00
        )

)

2 Comments

That works, but I don't have quotes on the keys on the incoming data?
Updated the answer
0

Try this

First make the string a valid json string.

Add [ at start and ] at end. So your code will be

$times = '[{endTime:"2017-03-29T17:15:00.000+11:00",startTime:"2017-03-29T17:00:00.000+11:00"},{endTime:"2017-03-31T17:15:00.000+11:00",startTime:"2017-03-31T17:00:00.000+11:00"},{endTime:"2017-04-01T12:15:00.000+11:00",startTime:"2017-04-01T12:00:00.000+11:00"}]';

$result = json_decode($times, true);

4 Comments

$times is NOT a valid json.
@u_mulder not yet but he can make it to one!
Even with [] it still not a valid json. Please test your code before answering.
the keys need to be enclosed with doublequotes ""
0

This is not valid JSON, it looks like a bunch of JS variables concatenated together. You need a heavily customised parser:

<?php
$times = '{endTime:"2017-03-29T17:15:00.000+11:00",startTime:"2017-03-29T17:00:00.000+11:00"},{endTime:"2017-03-31T17:15:00.000+11:00",startTime:"2017-03-31T17:00:00.000+11:00"},{endTime:"2017-04-01T12:15:00.000+11:00",startTime:"2017-04-01T12:00:00.000+11:00"}';
$times = str_replace(["{","}"],["\0","\0"],$times); //replace {} with null bytes (they're hopefully unique and will be used as delimiters)

$csv = str_getcsv($times,",","\0"); //Treat string as a CSV enclosed in those null bytes

$csv = array_map(function ($v) {
    $vars = explode(",",$v);
    $arr = [];
    foreach ($vars as $var) {
        $parts = str_getcsv($var,":","\""); // endTime:"2017-03-29T17:15:00.000+11:00" is like a CSV split by : and enclosed in "
        $arr[$parts[0]] = $parts[1];
    }
    return $arr;

},$csv);

print_r($csv);

Result:

Array
(
    [0] => Array
        (
            [endTime] => 2017-03-29T17:15:00.000+11:00
            [startTime] => 2017-03-29T17:00:00.000+11:00
        )

    [1] => Array
        (
            [endTime] => 2017-03-31T17:15:00.000+11:00
            [startTime] => 2017-03-31T17:00:00.000+11:00
        )

    [2] => Array
        (
            [endTime] => 2017-04-01T12:15:00.000+11:00
            [startTime] => 2017-04-01T12:00:00.000+11:00
        )

)

Alternately walk away from this mess and demand your data be properly formatted as JSON.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.