2011-11-15 81 views
0

我目前有以下陣列工作循環,通過數組的數組

Array 
(
    [0] => Array 
     (
      [0] => Array 
       (
        [candidate_id] => 41 
        [show_on_site] => urban talent 
        [first_name] => Barney 
        [surname] => Harwood 
        [gender] => male 
        [talent] => presenter 
        [skill] => 
        [DOB] => 1983-11-30 
        [Location] => London 
        [contact_telephone] =>
        [contact_email] => [email protected] 
        [height] => 5' 1" 
        [eyes] => Brown 
        [hair_colour] => brown 
        [hair_length] => medium 
        [accents] => Native Northern, others include - Yorkshire, Liverpool, Manchester, Birmingham, Cockney, RP, Welsh, Scottish, German, American 
        [training] => n/a 
        [unions] => Member of the British Academy of Composers & Songwriters 
Equity & MU Member 
        [visible] => yes 
        [availability] => yes 
        [availability_number] => 9999 
        [availability_order] => 0 
        [availability_comments] => BARNEY IS THE LATEST BLUE PETER PRESENTER AND CAN BE SEEN ON AIR MONDAYS & TUESDAYS AT 4.30PM ON BBC 
        [spotlight_url] => 
        [youtube_showreel] => 
        [date_created] => 2011-11-02 10:44:37 
        [created_by] => 1 
       ) 

     ) 

    [1] => Array 
     (
      [0] => Array 
       (
        [candidate_id] => 42 
        [show_on_site] => urban talent 
        [first_name] => Simon 
        [surname] => Ainley 
        [gender] => male 
        [talent] => actor 
        [skill] => 
        [DOB] => 1987-06-12 
        [Location] => Huddersfield 
        [contact_telephone] => 01484 532751 
        [contact_email] => [email protected] 
        [height] => 5' 1" 
        [eyes] => blue 
        [hair_colour] => brown 
        [hair_length] => short 
        [accents] => Accents 
        [training] => Training 
        [unions] => Union Membership 
        [visible] => yes 
        [availability] => yes 
        [availability_number] => 9999 
        [availability_order] => 0 
        [availability_comments] => Availability Comments 
        [spotlight_url] => http://www.google.com 
        [youtube_showreel] => http://www.youtube.com/watch?v=sP4NMoJcFd4 
        [date_created] => 2011-11-08 11:28:12 
        [created_by] => 1 
       ) 

     ) 

) 

,你可以看到它是一個數組類型的情況中的數組,我想遍歷它拉出來在first_name + surname每個條目的,但我得到以下錯誤,當我嘗試這樣做,

A PHP Error was encountered 

Severity: Notice 

Message: Undefined index: firstname 

Filename: admin/candidate_list.php 

Line Number: 5 

我通過它循環這樣的時刻,

<?php foreach ($candidates as $k => $v) : ?> 
    <li><a href="<?php echo site_url(); ?>candidates/card/id/<?php echo $v['id']; ?>"><?php echo $v[0]['first_name']. " ".$v[0]['surname']; ?></a></li> 

我在做什麼錯?

+2

你需要的是嵌套循環。循環內循環 – Ibu

+0

你確定你的代碼是最新的嗎?根據你想要訪問'$ v ['firstname']'而不是'$ v ['first_name']'的錯誤。 –

+0

@ sico87:很難從您提供的代碼中分辨出來。你能告訴我們你如何定義'候選人'嗎? –

回答

0

這會給你什麼?

<?php foreach ($candidates as $k => $v) print_r($v); ?> 

我不知道如何$ V [「身份證」]將被定義既考慮到陣列,所以有別的東西,你要離開了......

而且你使用SITE_URL ()...我聞到CodeIgniter的味道嗎? ;)

0

你的數組對於那個循環來說太深了。我假設$candidates是整個陣列的名字?你並不需要每個候選人都在他們自己的陣列中,試着讓你的結果放在一個低一級的數組中,並且foreach可以工作。

0
<? 
    for($i=0; $i=50; $i++){ 
    foreach ($candidates[$i] as $v){ 
    ?> 
     <li><a href="<?php echo site_url(); ?>candidates/card/id/<?php echo $v[0]['candidate_id']; ?>"><?php echo $v[0]['first_name']. " ".$v[0]['surname']; ?></a></li> 
    <? 
    } 
    } 
?> 
+1

'$ i => 0'?咦? –

+0

哎呀,對不起大聲笑 –

+0

'$ i = 50'?咦? (嘻嘻) –