2010-01-06 176 views
0

我目前正在构建codeigniter的网站上,目前我正在查询数据,我认为可能有3个数组可以作为数组返回不同的结果的量,我的问题我不能为我遍历数组我此刻的生活,通过多维数组的PHP循环

我的模型看起来像这样

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); 
} 

的获取返回数组看起来像这样,

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 
+1

你说的意思*我不能在此刻我遍历数组我的生活*。你不知道如何使用foreach?你不知道如何遍历多维数组?你有没有工作的代码?请更具体地说明你的问题。 – Gordon 2010-01-06 17:00:43

回答

2

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

foreach ($data['special'] as $row) { 
    // do stuff with the $row array 
} 
0

看起来像一个结果数组,在开始一个奇怪的元素。我想摆脱的第一个元素,然后就通过它的循环:

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

尝试的foreach

$arr = (your array); 

foreach ($arr as $key => $insideArrays) { 
foreach ($insideArrays as $k2 => $insideInsideArrays){ 
    .......... 
} 

} 
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(); 
} 

为此:

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 >"; 
     } 
    }