解析多维数组以查看codeigniter

时间:2016-11-09 19:19:24

标签: arrays multidimensional-array

在我的模型中有以下数组:

$data = [
            'route' => [
                    [
                        'id' => 1,
                        'departure_station' => 'Cluj-Napoca, Autogara',
                        'destination_station' => 'Bucuresti Nord',
                        'departure_date'  => 'Joi, 18 Mar 2017. 20:45',
                        'arrival_date' => 'Vi, 19 Mar 2017. 02:15',
                        'price' => 150
                    ],
                    [
                        'id' => 2,
                        'departure_station' => 'Budapest',
                        'destination_station' => 'London',
                        'departure_date'  => 'Vin, 28 Mar 2017. 20:45',
                        'arrival_date' => 'Lu, 29 Mar 2017. 02:15',
                        'price' => 250
                    ],
                    [
                        'id' => 3,
                        'departure_station' => 'Paris',
                        'destination_station' => 'Berlin',
                        'departure_date'  => 'Joi, 18 Mar 2017. 20:45',
                        'arrival_date' => 'Vi, 19 Mar 2017. 02:15',
                        'price' => 450
                    ],
            ]
        ];

我在我的控制器上获取它并在我var_dump($data)时正确显示 我尝试在我的视图中发送这个多维数组如下:

foreach($data as $key => $route)
        {

            foreach ($route as $element)
            {

                $id = $element['id'];
                $departure_station = $element['departure_station'];
                $destination_station = $element['destination_station'];
                $departure_date = $element['departure_date'];
                $arrival_date = $element['arrival_date'];
                $price = $element['price'];
                $i = count($route);

                for($j=0; $j<=$i; $j++)
                {
                    $html = $this->load->view("front/curse_interne.php", array(
                        'id' => $id,
                        'departure_station' => $departure_station,
                        'destination_station' => $destination_station,
                        'departure_date' => $departure_date,
                        'arrival_date' => $arrival_date,
                        'price' => $price), true);
                    echo json_encode(array("status" => 1, "html" => $html));
                }

            }
        }

我以这种方式在视图中收到了这个数组:

    <?php
$data = array('route'=>(array(array('id'=>$id, 'departure_station'=>$departure_station , 'destination_station'=>$destination_station, 'departure_date'=>$departure_date, 'arrival_date'=>$arrival_date, 'price'=>$price ))));

var_dump($data);
exit();

?>

我从视图中看到的var_dump只是第一个数组:

    array(1) {
  ["route"]=>
  array(1) {
    [0]=>
    array(6) {
      ["id"]=>
      int(1)
      ["departure_station"]=>
      string(21) "Cluj-Napoca, Autogara"
      ["destination_station"]=>
      string(14) "Bucuresti Nord"
      ["departure_date"]=>
      string(23) "Joi, 18 Mar 2017. 20:45"
      ["arrival_date"]=>
      string(22) "Vi, 19 Mar 2017. 02:15"
      ["price"]=>
      int(150)
    }
  }
}

如何在View中获取整个数组? 谢谢!

1 个答案:

答案 0 :(得分:1)

首先改变你的功能:

{
  "id": 1,
  "departure_station": "Cluj-Napoca, Autogara",
  "destination_station": "Bucuresti Nord",
  "departure_date": "Joi, 18 Mar 2017. 20:45",
  "arrival_date": "Vi, 19 Mar 2017. 02:15",
  "price": 150   
}

每个HTML( front / curse_interne.php )将获得其JSON对象,在您的情况下为3个对象:

{
  "id": 2,
  "departure_station": "Budapest",
  "destination_station": "London",
  "departure_date": "Vin, 28 Mar 2017. 20:45",
  "arrival_date": "Lu, 29 Mar 2017. 02:15",
  "price": 250
}

{
  "id": 3,
  "departure_station": "Paris",
  "destination_station": "Berlin",
  "departure_date": "Joi, 18 Mar 2017. 20:45",
  "arrival_date": "Vi, 19 Mar 2017. 02:15",
  "price": 450
}

  foreach($data as $route) {       
    $html = $this->load->view("front/curse_interne.php", array(
      'route' => $route
    ), true);
    echo json_encode(array("status" => 1, "html" => $html));
  }

然后在每个页面上,您应该只在PHP中获得一个不同的数组

为了获得整个数组,请将迭代逻辑更改为:

<?php
  $data = array('route' => $route);
  print('<pre>' . print_r($data, 1) . '</pre>');
  exit();
?>

并在您看来:

Array
  (
    [route] => Array
      (
        [0] => Array
            (
                [id] => 1
                [departure_station] => Cluj-Napoca, Autogara
                [destination_station] => Bucuresti Nord
                [departure_date] => Joi, 18 Mar 2017. 20:45
                [arrival_date] => Vi, 19 Mar 2017. 02:15
                [price] => 150
            )

        [1] => Array
            (
                [id] => 2
                [departure_station] => Budapest
                [destination_station] => London
                [departure_date] => Vin, 28 Mar 2017. 20:45
                [arrival_date] => Lu, 29 Mar 2017. 02:15
                [price] => 250
            )

        [2] => Array
            (
                [id] => 3
                [departure_station] => Paris
                [destination_station] => Berlin
                [departure_date] => Joi, 18 Mar 2017. 20:45
                [arrival_date] => Vi, 19 Mar 2017. 02:15
                [price] => 450
            )

    )

)

这应该会带回你原来的阵列:

{{1}}