I'm trying to output data into an array in a specific format but I'm stuck with the stmt->fetch part to get it to output the array in the format I require.
code I have to date (Cobbled together from various online sources)
private function get_results()
{
$parameters = array();
$results = array();
$mysqli = new mysqli('localhost', $this->user, $this->pass, $this->db)
or die('Problem connecting to the database');
$stmt = $mysqli->prepare($this->sql) or die('Problem preparing query, check SQL');
$stmt->execute();
$meta = $stmt->result_metadata();
while ( $field = $meta->fetch_field() ) {
$parameters[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $parameters);
while ( $stmt->fetch() ) {
$x = array();
foreach( $row as $key => $val ) {
$x[$key] = $val;
}
$results[] = $x;
}
$mysqli->close();
return $results;
}
Outputs the following:
Array ( [0] => Array ( [Topping] => Mushrooms [Slices] => 3 ) [1] => Array ( [Topping] => Olives [Slices] => 1 ) [2] => Array ( [Topping] => Onions [Slices] => 1 ) [3] => Array ( [Topping] => Pepperoni [Slices] => 1 ) [4] => Array ( [Topping] => Zuchini [Slices] => 2 ) )
but I want it in the following format:
Array ( [Topping] => Array ( [0] => Mushrooms [1] => Olives [2] => Onions [3] => Pepperoni [4] => Zuchini ) [Slices] => Array ( [0] => 3 [1] => 1 [2] => 1 [3] => 1 [4] => 2 ) )
I've been trying to achieve this for a while now but I can't seem to get my head around it. Any help would be much appreciated! Thanks.