Php 构建一个数组,在单引号中包含多个值,同时用逗号分隔每个值

Php 构建一个数组,在单引号中包含多个值,同时用逗号分隔每个值,php,arrays,sorting,Php,Arrays,Sorting,我希望我的最终结果是: Vendor1 => 'description1,description2,description3', Vendor2 => 'description4,description5,description6' 我现在有这样的东西: if(mssql_num_rows($execute)){ while($row = mssql_fetch_array($execute)){ $dropdown[$row['VendorName']] .

我希望我的最终结果是:

Vendor1 => 'description1,description2,description3',
Vendor2 => 'description4,description5,description6'
我现在有这样的东西:

if(mssql_num_rows($execute)){
   while($row = mssql_fetch_array($execute)){
        $dropdown[$row['VendorName']] .=  "'" . $row['Transaction Description'] . "',";
   }
}
但这给了我:

["Harland Financial Solutions"]=>
  string(54) "'Software-implemention/license/support','sfw support',"

在while循环结束时,使用php函数
$string=substr($string,0,-1)
。它将返回一个比原始字符串短一个字符的字符串(如果您正确填写了示例)。结果是一个字符串,最后一个逗号被截断。有关
substr()
的文档可在中找到


看起来您可以删除后面的逗号并删除单引号

if(mssql_num_rows($execute)){
   while($row = mssql_fetch_array($execute)){
        $dropdown[$row['VendorName']] .= $row['Transaction Description'].","
   }
   foreach($dropdown as $vendor => &$descriptions)
       $descriptions = substr($descriptions, 0, -1);
}
你可以做你喜欢的事情

foreach ($dropdown as $vendor=>&$data)
$data=mb_substr($data,0,-1);//to remove the last , that you don't need

那不是你在那里的东西吗?您指的是如何打印
$dropdown
?如果是这样,您需要向我们展示您用于打印其值的代码。foreach($vendor=>&$data)注意引用(&)忘记了引用:)如果我正确理解/想象了他的数据,他将需要循环使用可能的供应商值w/foreach$说明!=$描述点打开。对不起,睡眠不足。固定的。晚安!现在我投你一票
.
.
.
 $dropdown[$row['VendorName']] .=  $row['Transaction Description'] . ",";
.
.
.
foreach ($dropdown as $vendor=>&$data)
$data=mb_substr($data,0,-1);//to remove the last , that you don't need