php - 从 MySQL 查询构建数组?

标签 php mysql arrays

有人可以告诉我如何从 MySQL 查询创建数组吗?

数组应该是这样的:

<?php
$testLocs = array(
    '1' => array( 'info' => '1. New Random info and new position', 'lat' => 45345345, 'lng' => 44.9634 ),
    '2' => array( 'ino'  => '1. New Random info and new position', 'lat' => 686788787, 'lng' => 188.9634),
    '3' => array( 'info' => '1. New Random info and new position', 'lat' => 88888, 'lng' => 144.9634),
    '4' => array( 'info' => '1. New Random info and new position', 'lat' => 666666, 'lng' => 144.964 )
);
echo json_encode($testLocs);
exit();
?>

infotextlatlngnumbers 是MySQL $行

如有任何帮助,我们将不胜感激。

最佳答案

阅读manual

取自 PHP 手册的例子

$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);

关于php - 从 MySQL 查询构建数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31811680/

相关文章:

java - 如何?使用 Retrofit2 将当前日期从 Android 插入到 mySQL 数据库中

python - 查找一维数组/列表元素之间差异的函数,例如x[n]-x[n-1], x[n-2]-x[n-1] 其中 n 是数组/列表的大小

c - 解释为什么这两个多维数组看起来是有效的语法,但只有一个给出了预期的结果?

php - 从 php 嵌入变量

php - MySQL 文本字段中允许的最大大小

php - MySQL数据库表设置

sql - MySQL 字符串与百分比输出的比较

mysql - MySQL 查询中的 BREAK ON 指令?

c++ - 为什么有人告诉我数组是指针? C++中数组和指针的关系是什么?

php - 如何选择列值作为列名并将结果分组为一行