如何正确构建数组

时间:2012-03-29 03:03:06

标签: php

我遇到了将此数组转换为可以使用的格式的问题。我的foreach循环中没有显示其他数据。有人可以看看我构建阵列的方式并告诉我我做错了什么吗?

将有多个部门和多个地点。该位置取决于该部门的位置。

   foreach...
    $arr[$db['name']] =
      array($db['date'] =>
        array($db['dept'] => (Need other departments to show)
          array($db['location'] => $db) (Need more locations for above dept.)
        )
      );

给我:

Array
(
  [training] => Array
  (
    [08-24-2011] => Array
    (
      [dept1] => Array <----------There is more than one department that should show
      (
        [dept1 Other] => Array <--There is more than one location that should show
        (
          [start_time] => 03:00
          [end_time] => 19:00
        )
      )
    )
  )
)

这就是我所追求的。我只需要知道如何订购它以便我的数组产生这个putput;

Array
(
  [training] => Array
  (
    [08-24-2011] => Array
    (
      [dept1] => Array
      (
        [dept1 Other] => Array
        (
          [start_time] => 03:00
          [end_time] => 19:00
        )
        [dept2 Other] => Array
        (
          [start_time] => 03:00
          [end_time] => 19:00
        )
      )
      [dept2] => Array
      (
        [dept1 Other] => Array
        (
          [start_time] => 03:00
          [end_time] => 19:00
        )
        [dept2 Other] => Array
        (
          [start_time] => 03:00
          [end_time] => 19:00
        )
        [dept3 Other] => Array
        (
          [start_time] => 03:00
          [end_time] => 19:00
        )
      )
    )
  )
)

1 个答案:

答案 0 :(得分:0)

非循环obj或数组的var_dump是什么?

有几种可能性。这是对其中两个人的捅:

  1. 您循环的数据对于foreach循环来说不够用。我认为这是正在发生的事情:

    $arr = array();
    foreach( $dbObj as $db )
    {
      //your loop goes here
      //
    }
    
  2. 在这种情况下,您需要转储$ dbObj。它是一个2D阵列,就像你正在对待它?也许它真的是3D而且你没有足够的循环......

    1. 您只是通过查询从数据库中提取一个项目。检查......