为什么 foreach 只在数组中创建一个值?

Why foreach creating only one value in array?

我为 PrestaShop 1.6 创建了一个模块。我无法在 table 中获取此模块创建的列。我不知道可能出了什么问题。 '$fields' 只取最后一列。 '$result' 没问题。好像PHP没有创建索引。

public function getNewDescriptionFields()
{
    $db = Db::getInstance();
    if(!$db)
        return false;

    $result = $db->ExecuteS('SHOW COLUMNS FROM '. _DB_PREFIX_ .'product_lang');                 
    if(!$result)
        return false;

    $fields = array();
    foreach($result as $key => $field);
    {
        $fields[] = $field['Field'];
    }

    file_put_contents('/home/www/test4', var_export($fields, true));
    file_put_contents('/home/www/test3', var_export($result, true));
    $fields = preg_grep("/desc([0-9]?[0-9])_(name|text)/", $fields);
    return $fields;
}

来自测试 3(部分):

  19 => 
  array (
    'Field' => 'desc4_text',
    'Type' => 'text',
    'Null' => 'YES',
    'Key' => '',
    'Default' => NULL,
    'Extra' => '',
  ),
  20 => 
  array (
    'Field' => 'desc5_name',
    'Type' => 'varchar(64)',
    'Null' => 'YES',
    'Key' => '',
    'Default' => NULL,
    'Extra' => '',
  ),
  21 => 
  array (
    'Field' => 'desc5_text',
    'Type' => 'text',
    'Null' => 'YES',
    'Key' => '',
    'Default' => NULL,
    'Extra' => '',
  ),
  22 => 
  array (
    'Field' => 'desc6_name',
    'Type' => 'varchar(64)',
    'Null' => 'YES',
    'Key' => '',
    'Default' => NULL,
    'Extra' => '',
  ),
  23 => 
  array (
    'Field' => 'desc6_text',
    'Type' => 'text',
    'Null' => 'YES',
    'Key' => '',
    'Default' => NULL,
    'Extra' => '',

来自测试 3: array ( 0 => 'desc6_text', )

您正在使用;在 foreach 循环的末尾,循环体在第一个结束;所以请删除它。 foreach

的代码应该是这样的
foreach($result as $key => $field)
{
  $fields[] = $field['Field'];
}

Demo