0

I made a post regarding a code for Adobe Analytics API. I am using a code to retrieve the first and second element (dimension) which is called 'breakdown'. The number varies depending on the data we have for this element.

I am using a loop with i=10 but I want to take everything. Which is the best way to achieve that?

I am using this code:

foreach ($json->report->data as $element) 
{
        // putting an excessive number for i
        for ($i=0;$i<100;$i++)
        {
            // checking if the object exists
            if (isset($element->breakdown[0]->breakdown[$i]->name)) 
            {
                echo $element->breakdown[0]->breakdown[$i]->name;

                // putting the data in a list I created before
                array_push($list, array(''.$element->year.'-'.$element->month.'-'.$element->day, $element->breakdown[0]->name, $element->breakdown[0]->breakdown[$i]->name, $element->breakdown[0]->breakdown[$i]->counts[0], $element->breakdown[0]->breakdown[$i]->counts[1]));
            }
            else
            {
                // Break the loop. All the objects are in the row. So, if there is not any for i=45 then there won't be any for i>45
                continue;
            }

        }
}

where I am trying to get all the objects. I am checking if the objects exist. If they don't, I want this loop to stop (second loop).

Datacrawler
  • 2,780
  • 8
  • 46
  • 100

2 Answers2

2

Use break instead of continue to exit a loop

break stops the current loop continue goes to the next iteration of the loop.

To get out of both loops use break 2;

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
1

You can break the loop with break. for more information: http://php.net/manual/en/control-structures.break.php

foreach ($json->report->data as $element) 
{
    // putting an excessive number for i
    for ($i=0;$i<100;$i++)
    {
        // checking if the object exists
        if (isset($element->breakdown[0]->breakdown[$i]->name)) 
        {
            echo $element->breakdown[0]->breakdown[$i]->name;

            // putting the data in a list I created before
            array_push($list, array(''.$element->year.'-'.$element->month.'-'.$element->day, $element->breakdown[0]->name, $element->breakdown[0]->breakdown[$i]->name, $element->breakdown[0]->breakdown[$i]->counts[0], $element->breakdown[0]->breakdown[$i]->counts[1]));
        }
        else
        {
            // Break the loop. 
            break;
        }

    }

}

Niek de Gooijer
  • 654
  • 1
  • 9
  • 18
  • What is the difference between break and continue exactly? I thought that continue would stop the for loop and continue with the foreach. I also thought that break would just stop looping for the specific i. Am I wrong? – Datacrawler Dec 10 '15 at 10:29
  • It is in the name. Break "Breaks" the loops. While Continue goes to the next iteration. see: http://stackoverflow.com/questions/4364757/difference-between-break-and-continue-in-php – Niek de Gooijer Dec 10 '15 at 11:55