2016-12-04 92 views
0
array (size=2)  //top array containg an other arrays 
    0 => 
    array (size=2) //array containing 2 index 
    'content' => string '   The Lion King  ' (length=33) 
    'slide_style' => string '' (length=0) 
    1 => 
    array (size=2) 
    'content' => string '  Current fruit : mana bhanya the = 
    'slide_style' => string '' (length=0) 
    2 => 
    array (size=2) 
    'content' => string '  Current fruit : mana bhanya the king 
    'slide_style' => string '' (length=0) 

我有數組的類型結構,請告訴我,我怎麼能在我看來,文件訪問數據如何訪問數組中的數據放置在其他數組在php(codeigniter)?

回答

0

比方說,你的頂部陣列爲$陣列。

你可以遍歷數組訪問每個內部陣列及其項目:

foreach($array as $inner_array) { 
    echo $inner_array['content']; 
    echo $inner_array['slide_style']; 
} 

或者直接不通過頂部陣列循環訪問內部數組的特定值,你可以這樣做:

echo $array[0]['content']; 
echo $array[0]['slide_style']; 

在第二種方法中,您可以將0替換爲要訪問的任何內部數組(0表示第一個,1表示第二個等等)。

希望這會有所幫助。

+0

它的工作非常感謝:) – sani

+0

沒問題,很高興我可以幫助! :) –

+0

他不是指訪問通過控制器傳遞的視圖文件中的數組? – Perumal

0

將該數組作爲第二個參數傳遞給$this->load->view()

例如,有一個名爲posts.php的圖頁並通過控制器將數據傳遞給它如下所示:

$data['posts'] = $this->post_model->getPosts(); 

$this->load->view('posts.php', $data); 

並訪問它在視圖posts.php頁等:

foreach($posts as $post) { 
    // Here the other code goes ... 
} 

就像你在評論中提到的那樣,如果你想循環遍歷數組,這是你可能要找的代碼:

foreach($posts as $post) { 
    echo "<h2>{$post['title']}</h2>"; 
    echo "<p>{$post['description']}</p>"; 
} 

這就是你如何訪問多維數組。

希望你明白了。

+0

我只想循環2個數組 – sani

+0

你有多維數組? – Perumal

+0

是的,有多維數組 – sani