简体   繁体   English

替换为 SELECT GROUP_CONCAT 中的数组

[英]Replace with array from a SELECT GROUP_CONCAT

I am trying to do a str_replace without success.. for some reason the data from MySql isn't working inside the str_replace function...我正在尝试执行 str_replace 但没有成功。出于某种原因,来自 MySql 的数据在 str_replace function 中不起作用...

Code to bring all strings which will be used to replace the string:将所有字符串用于替换字符串的代码:

$aspas = "'";
$sql2 = '
SELECT
    GROUP_CONCAT(
        DISTINCT CONCAT("'.$aspas.'", prefixo, "-'.$aspas.','.$aspas.'-", posfixo, "'.$aspas.'") 
    ) AS prefixo_posfixo
FROM
    profissionais
';

$stm2 = $pdo->prepare($sql2);
$stm2->execute();
$resultado = $stm2->fetch();

Produces with no error this output:毫无错误地产生这个 output:

echo $resultado[0] >> 'dr-','-advogado','dra-','-advogada'

But when I try to insert inside the str_replace function:但是当我尝试在 str_replace function 中插入时:

$newstring = str_replace([$resultado[0]], '', 'dra-flavia-barao-advogada');
echo newstring >> dra-flavia-barao-advogada

As you see, the result keep the same, it doesn't replace the string;(如您所见,结果保持不变,它不会替换字符串;(

I think it is something about convert the array to string, but the $resultado[0] isn't in a array format so I cant implode...我认为这是关于将数组转换为字符串的事情,但是 $resultado[0] 不是数组格式所以我不能崩溃......

Do you know what I am doing wrong?你知道我做错了什么吗?

I forgot to post the solution before, There is:之前忘记贴解决方法了,有:

//SELECT THE STRINGS TO BE USED TO REMOVE FUNCTION
$sql2 = '
SELECT
    GROUP_CONCAT(
        DISTINCT CONCAT(prefixo, "-,-",posfixo) 
    ) AS prefixo_posfixo
FROM
    profissionais
';

$stm2 = $pdo->prepare($sql2);
$stm2->execute();
$prefixo_posfixo = $stm2->fetch();

//REPLACE / REMOVE THE STRINGS
$newstring = str_replace(explode(",", $prefixo_posfixo[0]), '', 'dra-flavia-barao-advogada');

//PRODUCES THE OUTPUT
flavia-barao

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM