[英]How to get data in array format in from php mysql database
I have the following function in PHP, I am trying to get array in a specific format mentioned below but to no success, I am selecting a specific column in the mysql table than contains a json value like我在 PHP 中有以下函数,我试图以下面提到的特定格式获取数组但没有成功,我在 mysql 表中选择了一个特定的列,而不是包含一个 json 值
{
"lat": "-0.008668899503063161",
"lng": "0.0025903204871857714"
}
This is the function这是功能
function test()
{
$pdo = new PDO ("mysql:host=localhost;dbname=dbname","user", "secret");
$statement=$pdo->prepare("SELECT test FROM merchants WHERE user_id = 1");
$statement->execute();
$data = array();
while( $row = $statement->fetch(PDO::FETCH_ASSOC) )
{
$data[] = json_decode( $row['test'] );
}
return $data ;
}
I am expecting the following result我期待以下结果
['lat' => -0.008668899503063161,'lng' => 0.0025903204871857714,]
But this is what I keep getting但这就是我不断得到的
[{"lat":"-0.008668899503063161","lng":"0.0025903204871857714"}]
Don't push onto an array if you just want a single value.如果您只想要一个值,请不要推送到数组。
function test()
{
$pdo = new PDO ("mysql:host=localhost;dbname=dbname","user", "secret");
$statement=$pdo->prepare("SELECT test FROM merchants WHERE user_id = 1");
$statement->execute();
row = $statement->fetch(PDO::FETCH_ASSOC) )
if ($row) {
$data = json_decode( $row['test'], true );
} else {
$data = null;
}
return $data ;
}
I managed to do it like this我设法这样做
'merchants.location' =>
[
'lat' => $data['lat'],
'lng' => $data['lng']
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.