代码之家  ›  专栏  ›  技术社区  ›  Udders

通过多维数组的PHP循环

  •  0
  • Udders  · 技术社区  · 14 年前

    我目前正在一个基于CodeIgniter的网站上工作,我目前正在查询数据,我认为可能有3个数组可以作为数组返回,每个数组都有不同数量的结果,我的问题是,我不能终生循环通过我目前拥有的数组,

    我的模型像这样

    public function get_special_backgrounds() {
        $this->db->select('*');
        $this->db->from('background');
        $this->db->where('is_special', 1);
    
        $query = $this->db->get();
        return $query->result_array();
    }
    

    我的控制器

    enter public function index() {
    //  $this->output->enable_profiler(TRUE);
        $data = array();
        if($query = $this->category_model->get_all_online()) {
            $data['main_menu'] = $query;
        }
        $this->load->model('image_model');
        /*
        * Sort out the users backgrounds, basically do a check to see if there is a 'special' background
        * if there is not a 'special' background then IF the user is logged in and has a background of there
        * own show that one, if not show a generic one, if they are not logged in show a generic one
        */
        $image = array();
        if ($query = $this->image_model->get_special_backgrounds()) {
            $image = $query;
        }
    
        $data = array_merge($data, $image);
        die(print_r($data));
        $this->load->view('home/main_page.php', $data);
    }
    

    gets返回的数组如下,

    Array
    (
        [main_menu] => CI_DB_mysql_result Object
            (
                [conn_id] => Resource id #28
                [result_id] => Resource id #35
                [result_array] => Array
                    (
                    )
    
                [result_object] => Array
                    (
                    )
    
                [current_row] => 0
                [num_rows] => 1
                [row_data] => 
            )
    
        [special] => Array
            (
                [0] => Array
                    (
                        [background_id] => 6
                        [background_name] => Master-Backgrounds.png
                        [background_path] => /Users/Simon/Sites/mysite/media/uploads/backgrounds/
                        [is_special] => 1
                        [background_date_uploaded] => 1262687809
                        [users_user_id] => 1
                        [users_user_group_group_id] => 1
                    )
    
                [1] => Array
                    (
                        [background_id] => 11
                        [background_name] => Master-mysite-Template.png
                        [background_path] => /Users/Simon/Sites/mysite/media/uploads/backgrounds/
                        [is_special] => 1
                        [background_date_uploaded] => 1262795313
                        [users_user_id] => 5
                        [users_user_group_group_id] => 2
                    )
    
            )
    
    )
    1
    
    4 回复  |  直到 13 年前
        1
  •  2
  •   Matteo Riva    14 年前

    你需要循环 special 数组的一部分?

    foreach ( $data['special'] as $row ) {
        // do stuff with the $row array
    }
    
        2
  •  3
  •   Nicholas    13 年前

    它是一个对象,所以不能像数组一样循环遍历它。我确实看到了你想做的事情,也明白了为什么这看起来是有道理的,但是要想知道我在说什么,试试这个:

    改变这一点:

    public function get_special_backgrounds() {
        $this->db->select('*');
        $this->db->from('background');
        $this->db->where('is_special', 1);
    
        $query = $this->db->get();
        return $query->result_array();
    }
    

    对此:

    public function get_special_backgrounds() {
        $this->db->select('*');
        $this->db->from('background');
        $this->db->where('is_special', 1);
    
        $query = $this->db->get();
        return $query;
    }
    

    改变这一点:

    $image = array();
    if ($query = $this->image_model->get_special_backgrounds()) {
        $image = $query;
    }
    

    对此:

       if($images = $this->image_model->get_special_backgrounds()) {
           foreach($images->result_array() as $image) {
              echo "<pre>";
              print_r($image);
              echo "</pre></br >";
           }
       }
    
        3
  •  1
  •   Natkeeran    14 年前

    尝试foreach

    $arr = (your array);
    
    foreach ($arr as $key => $insideArrays) {
     foreach ($insideArrays as $k2 => $insideInsideArrays){
      ..........
     }
    
    }
    
        4
  •  0
  •   leepowers    14 年前

    看起来像一个结果数组,开头有一个奇数元素。我将去掉第一个元素,然后循环使用它:

    array_shift($data);
    foreach ($data as $row) {
      // Do stuff with $row
      var_dump($row);
    }