php数组访问数据

时间:2013-11-19 10:06:26

标签: php arrays multidimensional-array

这是数组的一部分:

Array
(
    [0] => Array
        (
            [account_id] => 104318839768212
            [id] => act_104318839768212
            [adcampaigns] => Array
                (
                    [data] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 6011516331779
                                )

                            [1] => Array
                                (
                                    [id] => 6011399418379
                                )

                            [2] => Array
                                (
                                    [id] => 6008328196979
                                )

                        )

                    [paging] => Array
                        (
                            [cursors] => Array
                                (
                                    [after] => NjAwODMyODE5Njk3OQ==
                                    [before] => NjAxMTUxNjMzMTc3OQ==
                                )

                        )

                    [count] => 3
                    [limit] => 100
                    [offset] => 0
                )

        )

我正在尝试访问adcampaign下的ID并使用foreach循环列出它们 我试过这个:

 <?php foreach($ad_accounts as $ad_act): ?>
   <li><a href="#"><input type="radio" name="ad_act" value="<?php echo $ad_act['adcampaigns']['data']['id']; ?>" ><?php echo $ad_act['adcampaigns']['data']['id']; ?></a></li>
                      <?php endforeach; ?>

它返回white.any帮助

3 个答案:

答案 0 :(得分:2)

请尝试这样获得数据的ID值

<?
$ad_accounts = Array("0" => Array
        (
            "account_id" => 104318839768212,
            "id" => act_104318839768212,
            "adcampaigns" => Array
                (
                    "data" => Array
                        (
                            "0" => Array
                                (
                                    "id" => 6011516331779
                                ),

                            "1" => Array
                                (
                                    "id" => 6011399418379
                                ),

                            "2" => Array
                                (
                                    "id" => 6008328196979
                                )

                        ),

                    "paging" => Array
                        (
                            "cursors" => Array
                                (
                                    "after" => "NjAwODMyODE5Njk3OQ==",
                                    "before" => "NjAxMTUxNjMzMTc3OQ=="
                                )

                        )


                )

        )
);

foreach($ad_accounts[0]['adcampaigns']['data'] as $ad_act => $ad_value ){
   echo $ad_value['id'];
} ?>

答案 1 :(得分:1)

你需要另一个循环:

<?php foreach($ad_accounts as $ad_act):

    for ($i=0; $i < count($ad_act['adcampaigns']['data']); $i++) {  ?>
        <li><a href="#">
        <input type="radio" name="ad_act" value="<?php echo $ad_act['adcampaigns']['data'][$i]['id']; ?>" >
        <?php echo $ad_act['adcampaigns']['data'][$i]['id']; ?>
        </a></li>
    <?php }  ?>
<?php endforeach; ?>

答案 2 :(得分:-1)

你正在使用:在foreach之后

<?php foreach($ad_accounts as $ad_act): ?> 

更正此问题并尝试