2010-01-06 220 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 >"; 
     } 
    }