php - if-statement in a for(each)-loop. continue vs nothing -


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 create loop conditions

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

code above produce output 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 question: there differences (and mean performance) if i'll add continue statement within else statement ?

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

continue skips rest of code in current iteration, since there no code skip, second solution insignificantly slower (one more operation, , it's useless).

from php doc :

continue used within looping structures skip rest of current loop iteration , continue execution @ condition evaluation , beginning of next iteration.

it goto goes end of current loop iteration, useful in case :

foreach ($row $key => $value)  {     if ($value == "whatever")     {         continue;     }      function1($value);     // whatever code here      // continue skip above code } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -