PHP - 如何将多个类似JSON的字符串转换为关联数组?

时间:2017-03-28 07:39:27

标签: php arrays

输入字符串:

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

我试图将其转换为一个看起来像这样的数组:

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

)

我尝试过爆炸,组合和各种各样但我的代码非常混乱,以至于我确定必须有更好更清洁的方法吗?

这是 MY 最干净的起点,但即便这样也不干净,是吗?

$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>';

4 个答案:

答案 0 :(得分:3)

已完成改进:

  1. startingend附加的方括号。

  2. [a-zA-Z]+替换为"[a-zA-Z]+",使其成为有效json而不是json_decode

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

答案 1 :(得分:1)

此代码将取代&#39; endTime&#39;与&#39;&#34; endTime&#34;&#39;和startTime相同。我不建议你这样做,但在这种情况下它会对你有用:

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

这将输出:

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 :(得分:0)

试试这个

首先使字符串成为有效的json字符串。

在开始时添加[,在结尾添加]。所以你的代码将是

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

答案 3 :(得分:0)

这是无效的JSON,它看起来像一堆连接在一起的JS变量。您需要一个高度自定义的解析器:

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

结果:

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
        )

)

或者远离这个混乱,并要求将您的数据正确格式化为JSON。