這裏是一個示例代碼,它應該做你想要什麼:http://sandbox.onlinephpfunctions.com/code/4895a5b39c555835e378114eabc7ed78c6811285
<?php
// I create some sample data
$rows = [['sku' => 'a,b,c'], ['sku' => 'c,d,e'], ['sku' => 'e,f,g']];
$array = [];
// You want to replace this with your while loop
foreach ($rows as $row) {
$mark = explode(',', $row['sku']);
foreach ($mark as $out) {
if (!in_array($out, $array)) {
array_push($array, $out);
}
}
}
// I don't think you need this here, because you already ensure that the array won't contain duplicates
$unique_array = array_unique($array, SORT_REGULAR);
natsort($unique_array);
var_dump($unique_array);
var_dump($array);
因此,使用您的while ($row = mysql_fetch_array($sql))
它應該是這樣的:
<?php
$array = [];
while ($row = mysql_fetch_array($sql)) {
$mark = explode(',', $row['sku']);
foreach ($mark as $out) {
if (!in_array($out, $array)) {
array_push($array, $out);
}
}
}
natsort($unique_array);
var_dump($unique_array);
能不能請你格式化你的代碼幫助他人更好地理解它? – Cronco
我做了,請檢查它是否適用於您 – user3180708
看起來像'perl'。將其標記爲獲得正確的受衆羣體。 –