gpt4 book ai didi

PHP ARRAY 附带 "\"

转载 作者:行者123 更新时间:2023-11-29 21:43:26 29 4
gpt4 key购买 nike

我正在尝试从数据库获取一些标签并通过 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;

这是我用来从数据库获取数据并获取数据的 PHP 代码。

最佳答案

您可以使用 str_replace();stripslashes(); 与 stripslashes 它将是:

$output = stripslashes(array_slice($matches, 0, 10, true));
$value = implode(",", $output);

或分两个阶段

$output = array_slice($matches, 0, 10, true);
$output = stripslashes($output);
$value = implode(",", $output);

或使用str_replace()

$output = array_slice($matches, 0, 10, true);
$output = str_replace('\', '', $output);
$value = implode(",", $output);

关于PHP ARRAY 附带 "\",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34289095/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com