有递归数组对象的问题(stdClass对象)

时间:2013-05-01 14:27:40

标签: php

我希望有人可以帮我解决语法问题。我认为这是问题所在。我试图让循环显示所选对象,但它不起作用。这是我的代码。

stdClass Object
(
    [result_count] => 2
    [total_count] => 2
    [next_offset] => 2
    [entry_list] => Array
    (
        [0] => stdClass Object
            (
                [id] => da2b0ab5-873b-d9a8-0fcd-5180871e57eb
                [module_name] => Project
                [name_value_list] => stdClass Object
                    (

                        [description] => stdClass Object
                            (
                                [name] => description
                                [value] => soccer is fun!!!
                            )

                        [priority] => stdClass Object
                            (
                                [name] => priority
                                [value] => Primary
                            )

                        [total_years_played_c] => stdClass Object
                            (
                                [name] => total_years_played_c
                                [value] => 5
                            )

                        [sport_c] => stdClass Object
                            (
                                [name] => sport_c
                                [value] => Soccer
                            )

                        [high_school_exp_c] => stdClass Object
                            (
                                [name] => high_school_exp_c
                                [value] => 2
                            )

                        [varsity_exp_c] => stdClass Object
                            (
                                [name] => varsity_exp_c
                                [value] => default
                            )

                        [hand_right_c] => stdClass Object
                            (
                                [name] => hand_right_c
                                [value] => 0
                            )

                        [hand_left_c] => stdClass Object
                            (
                                [name] => hand_left_c
                                [value] => 1
                            )

                        [foot_right_c] => stdClass Object
                            (
                                [name] => foot_right_c
                                [value] => 1
                            )

                        [foot_left_c] => stdClass Object
                            (
                                [name] => foot_left_c
                                [value] => 0
                            )

                        [specialskills_c] => stdClass Object
                            (
                                [name] => specialskills_c
                                [value] => 
                            )

                        [varsity_starter_c] => stdClass Object
                            (
                                [name] => varsity_starter_c
                                [value] => 0
                            )

                    )

            )

        [1] => stdClass Object
            (
                [id] => 2e805552-c887-2b2e-a9e5-518087f3f87a
                [module_name] => Project
                [name_value_list] => stdClass Object
                    (

                        [description] => stdClass Object
                            (
                                [name] => description
                                [value] => I bleed for hockey... I will teach your kid to put them on the boards and take no prisoners!
                            )

                        [priority] => stdClass Object
                            (
                                [name] => priority
                                [value] => Secondary
                            )

                        [total_years_played_c] => stdClass Object
                            (
                                [name] => total_years_played_c
                                [value] => 7
                            )

                        [sport_c] => stdClass Object
                            (
                                [name] => sport_c
                                [value] => Hockey
                            )

                        [high_school_exp_c] => stdClass Object
                            (
                                [name] => high_school_exp_c
                                [value] => 3
                            )

                        [varsity_exp_c] => stdClass Object
                            (
                                [name] => varsity_exp_c
                                [value] => 2
                            )

                        [hand_right_c] => stdClass Object
                            (
                                [name] => hand_right_c
                                [value] => 0
                            )

                        [hand_left_c] => stdClass Object
                            (
                                [name] => hand_left_c
                                [value] => 1
                            )

                        [foot_right_c] => stdClass Object
                            (
                                [name] => foot_right_c
                                [value] => 1
                            )

                        [foot_left_c] => stdClass Object
                            (
                                [name] => foot_left_c
                                [value] => 0
                            )

                        [specialskills_c] => stdClass Object
                            (
                                [name] => specialskills_c
                                [value] => 
                            )

                        [varsity_starter_c] => stdClass Object
                            (
                                [name] => varsity_starter_c
                                [value] => 0
                            )

                    )

            )

    )

[relationship_list] => Array
    (
    )

)

这是我正在使用的PHP。现在,我只想回应sport_c的名字(一个应该是曲棍球,另一个应该是足球)

<?

foreach($response->entry_list as $sports) {
if(!empty($sports->name_value_list->sport_c)) {
   $sport_list = $sports->name_value_list->sport_c;
   $sport_name = $sport_list->value;
}
}

echo $sport_name;

?>

1 个答案:

答案 0 :(得分:0)

echo语句放在foreach循环中:

foreach($response->entry_list as $sports) {
    if(!empty($sports->name_value_list->sport_c)) {
        $sport_list = $sports->name_value_list->sport_c;
        $sport_name = $sport_list->value;
    }
    echo $sport_name;
}