【发布时间】:2015-12-15 12:17:35
【问题描述】:
我正在尝试从数据库中获取一些 Hashtags 并通过 json 编码发送到 ios 应用程序。
但不幸的是,我在数组中得到了一些“\”
{
"error_code": 0,
"response_string": "success.",
"result": "#manchesterunited\",\"#manunited\",\"#mufc\",\"#manchesterisred\",\"#manunitedisred\",\"#middleast"
}
但我需要以下值
{
"error_code": 0,
"response_string": "success.",
"result": {
"#manchesterunited",
"#manunited",
"#mufc",
"#manchesterisred",
"#manunitedisred",
"#middleast"
}
}
我的 php 代码
$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$params = json_decode($jsonInput,true);
$searchword = isset($params['hash'])?$params['hash']:'';
$sql = mysql_query("SELECT `hashtags` FROM `video`");
//Fetching the sql data
$hashtags = Array();
while ($row = mysql_fetch_array($sql)) {
$hashtags[] = $row['hashtags'];
}
$hashtags = array_unique($hashtags);
$string = rtrim(implode($hashtags));
$string = explode(",", $string);
$string = array_unique($string);
// Search the array with "preg_match" function.
$matches = array();
foreach($string as $k=>$v) {
if(preg_match("/^$searchword/", $v)) {
unset($matches[$k]);
$matches[$k] = $v;
}
}
$output = array_slice($matches, 0, 10, true);
$value = implode(",", $output);
echo json_encode(array("error_code"=>0,'response_string'=>'success.','result' =>$output));
exit;
这是我用于从 db 获取数据并获取数据的 PHP 代码。
【问题讨论】:
-
你能不能
echo$sql的结果,以便我们找出错误所在。 -
数据库中的数据有引号吗?
-
@bIgBoY
Array ( [0] => #fifa,#manchesterunited,#manunited,#talent,#mufc,#manchesterisred,#manunitedisred, [1] => [2] => [3] => #dubai,#emirates,#uae,#middleast, [4] => [5] => [6] => #Doha,#Timelaps, [7] => [8] => #blossom,#ultramotion, [9] => �#qatar,#qatarairways,#airbus350xwb, [10] => [11] => #qatar, ) -
$output = stripslashes($output);有帮助吗? -
str_replace('\', '',$output);也可能有效。
标签: php mysql arrays json web-services