2010-01-06 6 views
0

Je travaille actuellement sur un site qui est en cours de construction sur codeigniter, je suis actuellement en train d'interroger les données pour le moment je pense qu'il pourrait y avoir 3 tableaux qui pourraient être retournés en tableau avec un une quantité variable de résultats, ma question, je ne peux pas pour la vie de moi en boucle le tableau que j'ai en ce moment,PHP boucle à travers un tableau multidimensionnel

mon modèle ressemble à ceci

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

mon contrôleur

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

le tableau obtient le retour ressemble à ceci,

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

Que voulez-vous dire par * Je ne peux pas pour la vie de moi en boucle le tableau que j'ai en ce moment *. Vous ne savez pas comment utiliser foreach? Vous ne savez pas comment traverser les tableaux multidimensionnels? Vous avez du code qui ne fonctionne pas? Veuillez être plus précis sur votre problème. – Gordon

Répondre

2

avez-vous besoin de boucler sur la partie special du tableau?

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

On dirait un tableau de résultat, avec un élément étrange au début. Je me débarrasser du premier élément et puis juste boucle à travers elle:

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

Essayez foreach

$arr = (your array); 

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

} 
3

Il est un objet, vous ne pouvez pas en boucle à travers elle comme un tableau. Je ne vois ce que vous essayez de faire et de comprendre pourquoi il semble que cela fait sens, mais de voir ce dont je parle, essayez ceci:

Modifier ceci:

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

à ceci:

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

    $query = $this->db->get(); 
    return $query; 
} 

ET

Modifier ceci:

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

à ceci:

if($images = $this->image_model->get_special_backgrounds()) { 
     foreach($images->result_array() as $image) { 
      echo "<pre>"; 
      print_r($image); 
      echo "</pre></br >"; 
     } 
    } 
Questions connexes