【发布时间】:2017-12-21 23:35:33
【问题描述】:
我有一个数组,我正在使用 withJson() 在 JSON 中编码,但它引发了超薄应用程序错误。 下面是我的代码和数组值:
$db = Db::get_instance();
$stmt = $db->query($sql);
if($stmt->rowCount() > 0)
{
$dataList = $stmt->fetchAll(PDO::FETCH_ASSOC);
//print_r($dataList);die;
}
$data = array();
foreach($dataList as $row)
{ //print_r($row);
$id = '';
$name = '';
foreach($row as $key=>$value)
{
if($key == 'id') $id = $value;
if($key == 'countname') $countname = trim($value);
if($key == 'city') $city = trim($value);
if($key == 'region') $region = trim($value);
if($key == 'type')
{
if($value == 'country') $name="$countname ($value)";
if($value == 'state') $name = "$region, $countryname ($value)";
if($value == 'city') $name="$city,$region,$countname ($value)";
}
//$name = str_replace(',',' ',$name);
$temp['id'] = $id;
$temp['name'] = $name;
}
$data[] = $temp;
}print_r($data);die;
return $response->withJson($data,200);
请注意我的数组中有一些特殊字符:[7] => 数组 ( [id] => 4634 [名称] => Saint-g�rard,,Belgium (city) )
我的数组如下:
Array
(
[0] => Array
(
[id] => 65
[name] => Ura Vajgurore,,Albania (city)
)
[1] => Array
(
[id] => 2024
[name] => Birregurra,,Australia (city)
)
[2] => Array
(
[id] => 2703
[name] => Kallangur,,Australia (city)
)
[3] => Array
(
[id] => 985
[name] => Gurnitz,,Austria (city)
)
)
【问题讨论】:
-
显示什么错误?
-
您提到
slim application error被抛出但尚未将其添加到问题中。您能否提供错误消息,因为这可能会缩小问题范围。 -
error_reporting (E_ALL); ini_set('display_errors', 1);即使这段代码也没有显示任何错误。
-
删除
print_r($data);die; -
在代码中注释。这里没有注释只是为了显示数组值和格式