We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22980
    • 84 Posts
    This is a more general coding question than anything, but:

    is there any difference between this:
    $fields = $modx->db->getTableMetaData($table);
    foreach ($fields as $field){

    and this:
    foreach ( $modx->db->getTableMetaData($table) as $field){

    ? The main thing is that I want to be sure I’m not doing a db call w/ every iteration.

    --Peter
      • 6228
      • 249 Posts
      foreach operates on a copy of the array, and not the array itself. With that said, the foreach with the method invocation as the first parameter will be called once, and a copy of the result will be created and iterated over within the loop.

      Your two examples ’should’ execute with close to identical performance. You can always benchmark both examples to be sure, especially if you’re going to be iterating over hundreds of array values.

      I personally like the style of the latter example: assigning the array to a variable, and using the variable as a parameter for the foreach function. It looks cleaner, and makes for code that is easier to debug IMHO.
        lo9on.com

        MODx Evolution/Revolution | Remote Desktop Training | Development