使用PHP解码JSON

时间:2013-01-24 20:47:17

标签: php json

{"2":[[[["16","23:00"]],["18","22:00"]]],"1":[["20","20:00"]]}

我需要动态创建打印值,如下所示:

<p> 2 </p>
      <ul>
        <li>16 ---- 23:00 </li>
        <li>18 ---- 22:00 </li>
     </ul>
<p> 1 </p>
      <ul>
        <li>20 --- 20:00</li>
     </ul>

函数json_decode()返回的数组数据; 我怎么能像上面的例子那样打印...谢谢。 我知道我需要使用循环但是,我可以帮助构建它吗?

Array
(
    [2] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 16
                                    [1] => 23:00
                                )

                        )

                    [1] => Array
                        (
                            [0] => 18
                            [1] => 22:00
                        )

                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => 20
                    [1] => 20:00
                )

        )

)

问题,当我需要查看值时显示数组,返回如下:

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 14
                                    [1] => 23:00
                                )

                        )

                    [1] => Array
                        (
                            [0] => 19
                            [1] => 21:00
                        )

                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 15
                                    [1] => 23:00
                                )

                        )

                    [1] => Array
                        (
                            [0] => 17
                            [1] => 20:00
                        )

                )

        )

)

1

    Array----
    19----21:00

2

    Array----
    17----20:00

2 个答案:

答案 0 :(得分:2)

您可以使用json_decode函数

示例:

<?php
$a = json_decode('{"2":[[["16","23:00"],["18","22:00"]]],"1":[[["20","20:00"]]]}',true);
foreach($a as $i=>$b){
    echo "<p>";
    echo $i;
    echo "<ul>";
    foreach($b as $c){
        foreach($c as $d){
            echo "<li>";
            echo $d[0] . "----" . $d[1];
            echo "</li>";
        }
    }
    echo "</ul>";
    echo "</p>";
}
?> 

答案 1 :(得分:2)

您需要展平阵列。 试试这个:

<?
function array_flatten_recursive($array) {
   if (!$array) return false;
   $flat = array();
   $RII = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
   foreach ($RII as $value) $flat[] = $value;
   return $flat;
}

$jencoded = '{"2":[[[["16","23:00"]],["18","22:00"]]],"1":[["20","20:00"]]}';
$jdecoded = json_decode($jencoded);

foreach ($jdecoded as $paragraph => $paragraph_values) {
        echo "<p> $paragraph </p>\n";

        echo "   <ul>\n";
        $flattened = array_flatten_recursive($paragraph_values);
        for ($i=1; $i<count($flattened); $i+=2) {
                echo "      <li>";
                echo $flattened[$i-1];
                echo " ---- ";
                echo $flattened[$i];
                echo " </li>\n";
        }
        echo "   </ul>\n";
}
?>

根据问题作者的评论中的要求,下面的代码可用于按段落id对输出进行排序。每个段落数据按时间排序。

<?

$jencoded = '{"2":[[[["16","23:00"]],["18","22:00"]]],"1":[["20","20:00"]]}';

function array_flatten_recursive($array) { 
   if (!$array) return false;
   $flat = array();
   $RII = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
   foreach ($RII as $value) $flat[] = $value;
   return $flat;
}
function array_compare_by_time( $a, $b )
{
  if ($a->value == $b->value) { return 0; } 
  return ($a->value < $b->value) ? -1 : 1;
}

function array_sort_by_time($array) {
        //print_r($array);
        $a = array();

        for ($i=1; $i<count($array); $i+=2) {
                $o = new stdClass;
                $o->key = $array[$i-1];
                $o->value = $array[$i];
                $a[] = $o;
        }
        usort($a,'array_compare_by_time');
        return (array) array_flatten_recursive((array)$a);
} 

$jdecoded = (array)json_decode($jencoded);
asort($jdecoded, SORT_NUMERIC);

foreach ($jdecoded as $paragraph => $paragraph_values) {
        echo "<p> $paragraph </p>\n";

        echo "   <ul>\n";
        $flattened = array_flatten_recursive($paragraph_values);
        $flattened = array_sort_by_time($flattened);

        for ($i=1; $i<count($flattened); $i+=2) {
                echo "      <li>";
                echo $flattened[$i-1];
                echo " ---- ";
                echo $flattened[$i];
                echo " </li>\n";
        }

        echo "   </ul>\n";
}
?>

输出现在是:

<p> 1 </p>
   <ul>
      <li>20 ---- 20:00 </li>
   </ul>
<p> 2 </p>
   <ul>
      <li>18 ---- 22:00 </li>
      <li>16 ---- 23:00 </li>
   </ul>