2013-05-07 17 views
-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'," 
+0

的,你可以做未便是不是你有什麼呢?你是指「$ dropdown」是如何打印的?如果是這樣,你需要告訴我們你用什麼代碼打印出它的值。 – thatidiotguy 2013-05-07 21:39:01

回答

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); 
} 
+0

$ descriptions!= $ description – CoderOfHonor 2013-05-07 21:53:38

+1

spot on。對不起,睡眠不足。固定。晚安! – 2013-05-07 21:57:33

+0

現在你有我的投票。 :) – CoderOfHonor 2013-05-07 22:00:33

0

在while循環結束後,使用PHP函數$string = substr($string, 0, -1)。它將返回一個比原始字符短一個字符的字符串(如果您正確填寫示例)。結果是最後一個逗號被切斷的字符串。有關substr()的文檔可以在PHP manual中找到。

if(...){ 
    while(...){ 
     ... 
     foreach($dropdown as &$string) 
     $string = substr($string, 0, -1); 
    } 
} 
+0

如果我理解/想象他的數據正確,他需要循環可能的供應商值w/foreach – 2013-05-07 21:46:02

+0

好的,修復它。 :) – CoderOfHonor 2013-05-07 21:52:52

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 
+0

foreach($ dropdown爲$ vendor =>&$ data)//注意參考(&) – 2013-05-07 21:44:35

+0

忘記了參考:) – Nikola 2013-05-07 21:45:13