Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

this is a part of the 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
                )

        )

I am trying to access the ids under adcampaign and list them with a foreach loop I tried this :

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

it returns white.any help please

share|improve this question
1  
try a iterator in loop like $ad_act['adcampaigns']['data'][$i]['id'] –  rakimo Nov 19 '13 at 10:13

3 Answers 3

up vote 1 down vote accepted

You need another loop :

<?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; ?>
share|improve this answer
    
your code has mistakes. you closed the php tag and then using for ? –  cppit Nov 19 '13 at 10:14
    
@fogsy Fixed now. Thanks. –  Fouad Fodail Nov 19 '13 at 10:16
    
perfect thank you! –  cppit Nov 19 '13 at 10:24
    
You are welcome mate :) –  Fouad Fodail Nov 19 '13 at 10:25

Please try this you get ID value of data

<?
$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'];
} ?>
share|improve this answer

You are using : after the foreach

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

correct this and try

share|improve this answer
    
that's the correct way to do a foreach / endforeach –  cppit Nov 19 '13 at 10:13
    
You should probably check the alternative syntax for PHP. –  Ben Fortune Nov 19 '13 at 10:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.