有没有办法估计PHP数组将消耗多少内存?

Suppose that you have a query and you know exactly how many results it will return and length of every column, e.g. 1M records int `id`, char(100) `uid`.

Is there a way to know in advance how much memory will PHP need to allocate for this associative array?

Just don't allocate 1M records as an associative array. Select only data you need.

Best way to find out would be to do something like:

$numItems = 1000000;
$bytes = memory_get_usage();
$arr = array();
$arrBytes = memory_get_usage() - $bytes;
$arr[] = array('id'=>1,'uid'=>str_repeat("a",100));
$itemBytes = memory_get_usage() - $bytes - $arrBytes;
echo "Total memory usage estimate: ".($arrBytes + ($itemBytes * $numItems));

Disclaimer: I'm not completely sure how accurate this will be but should give a ballpark. This will also vary on different systems (i.e. 32bit ints versus 64 bit ints) and different encodings (i.e. single byte versus multi byte).

Having said all this (and as @YourCommonSense noted). The answer to "how much memory will a million item array consume" is "too much". Consider paginating or the like to reduce the number of items you need.