从多维数组中检索结果

时间:2014-01-08 10:24:28

标签: php arrays multidimensional-array foreach

这是我的第一篇文章,所以你好!

想知道我可以从这个md数组中得到我的结果吗?它给了我意想不到的结果。我找了答案,但不确定我应该问什么问题..

这是数组[使用var_dump]:

的相关部分的示例
["Attendee"] => array(11) 
                  { 
                    ["id"]=> int(148) 
                    ["firstname"]=> string(5) "dave" 
                    ["lastname"]=> string(6) "davey"`
                  }

这是我的foreach代码,用于获取每位与会者的名字和姓氏:

foreach ($the_info_array['body']['Registrations'] as $registrations)
{
    foreach ($registrations as $regvalue)
    {
        echo $regvalue['firstname'].” “.$regvalue['lastname'];
    }
    echo “<br>”;
}

打印:

a a2 2 5 5 dave davey
a a2 2 5 5 scott davey
a a2 2 5 5 bill davey

而不是我想要的东西,即:

dave davey
scott davey
bill davey

我哪里错了,&amp;什么是额外的字符?
在此先感谢社区给予的任何帮助!

编辑&gt;&gt;

奇怪的是,如果我将回声改为

echo $regvalue['firstname'];

打印:

a25dave
a25scott
a25bill

echo $ regvalue ['firstname']。“”;

打印

a 2 5 dave
a 2 5 scott
a 2 5 bill

这是完整的数组减去几条记录,因为我似乎无法发布那么多字符:

array(3) { 
    ["status"]=> string(2) "OK" 
    ["status_code"]=> int(200) 
    ["body"]=> array(1) { 
        ["Registrations"]=> array(12) { 
            [0]=> array(15) { 
                ["id"]=> int(148) 
                ["status"]=> string(8) "approved" 
                ["date_of_registration"]=> string(19) "2013-12-04 12:43:31" 
                ["final_price"]=> int(0) 
                ["code"]=> string(23) "529f2373bb7555.08033147" 
                ["url_link"]=> string(0) "" 
                ["is_primary"]=> bool(true) 
                ["is_group_registration"]=> bool(true) 
                ["is_going"]=> bool(true) 
                ["is_checked_in"]=> bool(false) 
                ["Event"]=> array(14) { 
                    ["id"]=> int(531) ["code"]=> string(15) "1-5298c3675e610" 
                    ["name"]=> string(21) "Recital 2014" 
                    ["description"]=> string(721) "The Description is this!" 
                    ["status"]=> string(6) "active" 
                    ["limit"]=> int(280) 
                    ["group_registrations_allowed"]=> bool(true) 
                    ["group_registrations_max"]=> int(50) 
                    ["active"]=> bool(true) 
                    ["member_only"]=> bool(false) 
                    ["virtual_url"]=> string(0) "" 
                    ["call_in_number"]=> string(0) "" 
                    ["phone"]=> string(0) "" 
                    ["metadata"]=> array(9) { 
                        ["default_payment_status"]=> string(10) "Incomplete" 
                        ["venue_id"]=> int(1) 
                        ["additional_attendee_reg_info"]=> string(1) "1" 
                        ["add_attendee_question_groups"]=> array(1) { 
                            [1]=> string(1) "1" } 
                        ["date_submitted"]=> string(10) "29/11/2013" 
                        ["event_hashtag"]=> string(0) "" 
                        ["event_format"]=> string(0) "" 
                        ["event_livestreamed"]=> string(0) "" 
                        [""]=> string(0) "" } } 
                ["Attendee"]=> array(11) { 
                    ["id"]=> int(219) 
                    ["firstname"]=> string(5) “dave” 
                    [“lastname”]=> string(7) "davis" 
                    ["address"]=> string(13) "10 The Street" 
                    ["address2"]=> string(0) "" 
                    ["city"]=> string(8) "The City" 
                    ["state"]=> string(10) "The County" 
                    ["country"]=> string(0) "" 
                    ["zip"]=> string(6) "BN66YY" 
                    ["email"]=> string(17) "mail@webhost.com" 
                    ["phone"]=> string(12) "012736654432" } 
                ["Transaction"]=> array(9) { 
                    ["id"]=> int(219) 
                    ["timestamp"]=> string(19) "2013-12-23 00:26:22" 
                    ["total"]=> float(132.6) 
                    ["amount_paid"]=> int(0) 
                    ["status"]=> string(10) "incomplete" 
                    ["details"]=> string(0) "" 
                    ["tax_data"]=> string(0) "" 
                    ["session_data"]=> string(0) "" 
                    ["payment_gateway"]=> string(19) "Credit / Debit Card" } 
                ["Datetime"]=> array(8) { 
                    ["id"]=> int(0) ["is_primary"]=> bool(true) 
                    ["event_start"]=> string(19) "2014-01-11 00:00:00" 
                    ["event_end"]=> string(19) "2014-01-11 00:00:00" 
                    ["registration_start"]=> string(19) "2012-08-12 16:00:00" 
                    ["registration_end"]=> string(19) "2014-01-10 16:00:00" 
                    ["limit"]=> int(280) ["tickets_left"]=> int(274) } 
                ["Price"]=> array(9) { 
                    ["id"]=> int(0) 
                    ["name"]=> string(0) "" 
                    ["amount"]=> int(0) 
                    ["description"]=> string(0) "" 
                    ["limit"]=> int(9999999) 
                    ["remaining"]=> int(999999) 
                    ["start_date"]=> NULL 
                    ["end_date"]=> NULL 
                    ["Pricetype"]=> array(8) { 
                        ["id"]=> int(1) 
                        ["name"]=> string(10) "Base Price" 
                        ["is_member"]=> bool(false) 
                        ["is_discount"]=> bool(false) 
                        ["is_tax"]=> bool(false) 
                        ["is_percent"]=> bool(false) 
                        ["is_global"]=> bool(true) 
                        ["order"]=> int(0) } } } } } } 

//此外,不知道是否相关,但数组来自最初的json解码

1 个答案:

答案 0 :(得分:0)

问题是你在注册中的每个数组上循环,即使其中大多数都不是参与者(即$regvalue将是第二个循环e.t.c中第一个循环状态的id。)。

尝试:

foreach ($the_info_array['body']['Registrations'] as $registrations)
{
        echo $registrations['Attendee']['firstname']." ".$registrations['Attendee']['lastname'];
    echo "<br>";
}