-2

我希望我的最终结果看起来像:

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',"
4

3 回答 3

2

似乎您可以删除尾随逗号并删除单引号。

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);
}
于 2013-05-07T21:42:31.193 回答
0

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

if(...){
   while(...){
      ...
      foreach($dropdown as &$string)
         $string = substr($string, 0, -1);
   }
}
于 2013-05-07T21:41:57.487 回答
0
.
.
.
 $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
于 2013-05-07T21:43:43.170 回答