I have written a function that I can pass any mysql query to (well, I haven't tested extensively but I haven't broken it so far) and it returns a multidimensional array with the fields and values. I would like some feedback to see if there is possibly a better way to do this or to store the array.
In a nutshell the query runs, it stores the field names in $array['fields'] and the data in $array['values'][$a] (where $a increments each loop).
The bottom function will accept a multidimensional array and loop recursively until it finds a value that isn't an array and then returns that bottom level of the array. This will fall apart if the "bottom level" of the array has some values that are arrays and some that are not. I'm sure there is a better way to do this as it is very specific to how I have my array set up (maybe that is ok though).
Any help or suggestions on how to run the sq function is appreciated. Is there a better way to store the resulting array?
Suggestions regarding the array_drill_down function is appreciated too although I am not as concerned with this and it will probably largely be unnecessary to use that with the direction I am going.
[code=php]function sq($qs) {
//initialize the array
$qdata=array();
//execute the query
$query = mysql_query($qs);
//read the field names
while($field = mysql_fetch_field($query))
$qdata['fields'][] = $field->name;
//set the iteration var
$a=0;
//loop through the results and put everything into qdata
while($results = mysql_fetch_array($query)){
for($i=0;$i<count($qdata['fields']);$i++)
$qdata['values'][$a][] = $results[$i];
$a++;
} //end while
return $qdata;
}[/code]
[code=php]/* Strip a multidimensional array down to the lowest level and
* returns a multidimensional array with 2 levels
* returns a named array
*/
function array_drill_down($array){
$thisarray = array();
foreach($array as $a=>$b){
if (is_array($b))
parse_array_lowest($b);
else {
$thisarray[] = $b;
}
}
return $thisarray;
}[/code]