-5
{"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
4

2 回答 2

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>
于 2013-01-24T21:39:11.643 回答
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>";
}
?> 
于 2013-01-24T20:51:54.533 回答