无法将两个数组合并为一个来获取sql查询

Its might sounding like a noob question but I can't find an answer. Currently I have this.

$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $gridData[] = array(
                'CompanyName' => $row['CompanyName'],
                'ContactName' => $row['ContactName'],
                'ContactTitle' => $row['ContactTitle'],
            'Address' => $row['Address'],
            'Country' => $row['Country'],
            'Parent' => $row['Parent'],
            'City' => $row['City'],
            'id' => $row['id']
      );
}

but assuming I don't want to hardcode this fields there I have another array which is stored in $fields and has the structure of an array inside so field [0] is the fieldname.

so I do

 foreach ($fields as $field)
     {
       $gridData[$field[0]] => $row[$field[0]];
     }

by some reason it does not work like this. what is the way to do this properly? How can I bring this together?

$gridData[] = array() 

will add a numerical index to the $gridData array with value of what is in the array. You are trying to do

$gridData[$field[0]] => $row[$field[0]];

which is trying to reference a string index. Try:

foreach ($gridData as $rowIndex => $gridRow){
  foreach ($fields as $field)
  {
    $gridData[$rowIndex][$field[0]] => $row[$field[0]];
  }
}

Alternatively, if you would be ok modifying your field data structure, you could use http://php.net/manual/en/function.array-merge.php

This way:

$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
            $gridData[] = $row;

Or this way:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
   array_push($gridData, $row);

EDIT : If you really want to only get some columns of your table, you have to filter them before in the query:

$fields=Array("name", "firstname");
$query="SELECT ".join(",", fields)." FROM table WHERE";

Anf if your structure of $fields is, as said in a previous comments, an array of array $fields=Array(Array("name"), Array("firstname")); you can extract a well formatted array for the previous command with array_map(create_function('$a', "return $a[0];"), $fields)

$i=0;
$e=count($fields);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {


        $gridData[$i]=array( );

        $e=0;
        $fin=count($fields);
        //echo "<h1>".$fin."</h1>";
        while ($e < $fin){
        $gridData[$i]=array_merge($gridData[$i], array( $fields[$e][0] => $row[$fields[$e][0]]));
        $e++;
        }

         $i++;}

I am quite not sure why this works