如何在PHP数组中显示唯一值?

时间:2023-01-16 12:42:56

I want to display unique customer values in array, below is the code;

我想在数组中显示唯一的客户值,下面是代码;

$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0)
    {
        while ($rad = mysql_fetch_array($sql_result)){ echo $rad['customer'];}
    }

I'll be grateful if any one can guide me, how to display unique values?

如果有人能指导我,如何展示独特的价值观,我将不胜感激?

1 个解决方案

#1


0  

Try something like this:

尝试这样的事情:

$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL     query" '.$sql);


$customers = array();

if (mysql_num_rows($sql_result)>0) {
    while ($rad = mysql_fetch_array($sql_result)) { 
        echo $rad['customer'];
        $customers[] = $rad['customer'];
    }
}

$uniqueCustomers = array_unique($customers);

Or if you have a unique ID

或者,如果您有唯一的ID

if (mysql_num_rows($sql_result)>0) {
    while ($rad = mysql_fetch_array($sql_result)) { 
        echo $rad['customer'];
        $customers[$rad['customerId']] = $rad['customer'];
    }
}

#1


0  

Try something like this:

尝试这样的事情:

$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL     query" '.$sql);


$customers = array();

if (mysql_num_rows($sql_result)>0) {
    while ($rad = mysql_fetch_array($sql_result)) { 
        echo $rad['customer'];
        $customers[] = $rad['customer'];
    }
}

$uniqueCustomers = array_unique($customers);

Or if you have a unique ID

或者,如果您有唯一的ID

if (mysql_num_rows($sql_result)>0) {
    while ($rad = mysql_fetch_array($sql_result)) { 
        echo $rad['customer'];
        $customers[$rad['customerId']] = $rad['customer'];
    }
}