mardi 26 juillet 2016

Foreach+switch loop or isset($_REQUEST[]) or Foreach+if-else

I was wondering, in terms of performance...

If I had an $_REQUEST array of 30 elements, and I needed to grab elements to set them to new arrays using functions, when would it be better to use multiple ISSET vs FOREACH+SWITCH:

Example:

  • Method 1 = 15 elements
  • Method 2 = 10 elements
  • Method 3 = 2 elements

Method 1:

foreach($_REQUEST['data'] as $key => $val)
{
    switch($key){
    case 'blah':
    $newarray['blah'] = $val;
    break;
    .....
    case 'blah2':
    .....
    case 'blah3':
    .....
    case 'blah4':
    .....
    }
}

Method 3:

if(isset($_REQUEST['data']['blah'])) $newarray['blah'] = $_REQUEST['data']['blah'];
if(isset($_REQUEST['data']['blah2'])) $newarray['blah2'] = $_REQUEST['data']['blah2'];

I would assume for method 3 it is better to use if(isset) but I am not sure, when would it be good to use one over the other?

Aucun commentaire:

Enregistrer un commentaire