我正在尝试以特定格式将数据输出到数组中,但我坚持使用 stmt->fetch 部分以使其以我需要的格式输出数组。
我必须迄今为止的代码(从各种在线资源拼凑而成)
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;
}
输出以下内容:
大批
(
[0] => 数组
(
[浇头] => 蘑菇
[切片] => 3
)
[1] => 数组
(
[浇头] => 橄榄
[切片] => 1
)
[2] => 数组
(
[浇头] => 洋葱
[切片] => 1
)
[3] => 数组
(
[浇头] => 意大利辣香肠
[切片] => 1
)
[4] => 数组
(
[打顶] => 西葫芦
[切片] => 2
)
)
但我希望它采用以下格式:
大批
(
[顶部] => 数组
(
[0] => 蘑菇
[1] => 橄榄
[2] => 洋葱
[3] => 意大利辣香肠
[4] => 小南瓜
)
[切片] => 数组
(
[0] => 3
[1] => 1
[2] => 1
[3] => 1
[4] => 2
)
)
我一直在努力实现这一目标,但我似乎无法理解它。任何帮助将非常感激!谢谢。