Question

Let's consider following array:

$data = array(
    '0' => array(
        'id' => '0',
        'guid' => '22dd39bf-f6d6-4283-b87c-370354a7c2dd',
        'age' => '32',
        'name' => 'Harriet Vazquez',
        'gender' => 'female',
        'email' => 'harrietvazquez@applica.com',
        'tags' => array(
            '0' => 'sit',
            '1' => 'mollit',
            '2' => 'cillum',
            '3' => 'irure',
        ),
        'friends' => array(
            '0' => array(
                'id' => '0',
                'name' => 'Long Dejesus',
            ),
            '1' => array(
                'id' => '1',
                'name' => 'Carrillo Hodge',
            ),
            '2' => array(
                'id' => '2',
                'name' => 'Coffey Greene',
            ),
            '3' => array(
                'id' => '3',
                'name' => 'Stephanie Chavez',
            ),
            '4' => array(
                'id' => '4',
                'name' => 'Richmond Mitchell',
            ),
        )
    ),
    '1' => array(
        'id' => '1',
        'guid' => '3df3ae55-03f3-4d7d-9c70-c7010a100886',
        'age' => '36',
        'name' => 'David Lynch',
        'gender' => 'male',
        'email' => 'davidlynch@applica.com',
        'tags' => array(
            '0' => 'id',
            '1' => 'ad',
            '2' => 'labore',
            '3' => 'ad',
            '4' => 'veniam',
            '5' => 'nulla',
        ),
        'friends' => array(
            '0' => array(
                'id' => '0',
                'name' => 'Diana Watts',
            ),
            '1' => array(
                'id' => '1',
                'name' => 'Patty Crawford',
            ),
            '2' => array(
                'id' => '2',
                'name' => 'Terrell Larson',
            ),
        )
    ),
    '2' => array(
        'id' => '2',
        'guid' => 'da2c9f3f-ac85-4dfd-a43c-e55e476596ca',
        'age' => '25',
        'name' => 'Hardin Murphy',
        'gender' => 'male',
        'email' => 'hardinmurphy@applica.com',
        'tags' => array(
            '0' => 'laborum',
            '1' => 'labore',
            '2' => 'dolor',
            '3' => 'excepteur',
            '4' => 'est',
        ),
        'friends' => array(
            '0' => array(
                'id' => '0',
                'name' => 'Mandy Roberts',
            ),
            '1' => array(
                'id' => '1',
                'name' => 'Walker Young',
            ),
            '2' => array(
                'id' => '2',
                'name' => 'Middleton Baldwin',
            ),
            '3' => array(
                'id' => '3',
                'name' => 'Tillman Harmon',
            ),
        )
    )
);

Let us now create a loop with some conditions

$accepted = array('age', 'name', 'email');
foreach ($data as $idx => $row) 
{
    foreach ($row as $key => $value) 
    {
        if(!is_array($value) && in_array($key, $accepted))
        {
            var_dump($value) . PHP_EOL;

        }
    }
}

Code above will produce output like this:

string(2) "32"
string(15) "Harriet Vazquez"
string(26) "harrietvazquez@applica.com"
string(2) "36"
string(11) "David Lynch"
string(22) "davidlynch@applica.com"
string(2) "25"
string(13) "Hardin Murphy"
string(24) "hardinmurphy@applica.com"

And now the question: Is there are any differences (and i mean performance) if I'll add an continue statement within else statement ?

$accepted = array('age', 'name', 'email');
foreach ($data as $idx => $row) 
{
    foreach ($row as $key => $value) 
    {
        if(!is_array($value) && in_array($key, $accepted))
        {
            var_dump($value) . PHP_EOL;

        }
        else
        {
            continue;
        }
    }
}
Was it helpful?

Solution

Continue skips the rest of the code in the current iteration, since there is no code to skip, your second solution will be insignificantly slower (one more operation, and it's useless).

From the PHP doc :

continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

It it basically a goto that goes to the end of your current loop iteration, it would be useful in a case like this :

foreach ($row as $key => $value) 
{
    if ($value == "whatever")
    {
        continue;
    }

    function1($value);
    // Whatever code here

    // The continue will skip the above code
}

OTHER TIPS

Yes, there is a slight performance difference: The latter version will run O(1) slower, for a very small value of 1.

The reason is, that the parsing (or might as well call it "compiling") stage of the script will run a tiny bit slower, but I am quite sure the bytecode from both versions will have identical runtime properties (if it is not identical).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top