用数组循环遍历数组



我目前正在处理以下数组,

    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] => 01234 567890
                    [contact_email] => barney@bluepeter.co.uk
                    [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] => simonainley@the-factory.co.uk
                    [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>

我做错了什么?

这给了你什么?

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

我不知道$v['id']将如何定义给定的数组,所以还有一些东西你遗漏了…

你正在使用site_url()…我闻到CodeIgniter的味道了吗?div;)

你的数组太深,不适合这个循环。我假设$candidates是整个大数组的名称?您不需要每个候选对象在自己的数组中,尝试在数组中获得少一个级别的结果,并且foreach将工作。

 <?
    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>
    <?
    }
    }
?>

相关内容

  • 没有找到相关文章

最新更新