如何在php中对一个数组进行降序排序?

问题描述 投票:0回答:1

新进入的产品会显示在最后一行。如果数量较多,在底部很难看到。怎样才能把新的显示改为最上面。

$barcodes_ary = explode(',', $barcodes);
    $barcodes_hash = array ();
    foreach ($barcodes_ary as $b) {
        if (array_key_exists($b, $barcodes_hash)) {
            $barcodes_hash[$b] += 1;
        } else {
            $barcodes_hash[$b] = 1;
        }
    }
    foreach ($barcodes_hash as $b => $amount) {
        if ($barcodes_ary == array(''))continue; 
        $ary = get_sql("SELECT * FROM Products WHERE Barcode='$b' Order by ProductName");
                    if ( count($ary) == 0 ) {
            continue;
        } 
        $pid  = $ary[0]['ProductID'];
        $pn   = $ary[0]['ProductName'];
        $unit = $ary[0]['UnitID'];

        $ary2 = get_sql("SELECT UnitName FROM Units WHERE UnitID = '$unit'");
        $unit = $ary2[0]['UnitName'];

        ?>
        <TR>
        <TD><? echo "$pn"; ?></TD>
        <TD>
        <INPUT TYPE=hidden NAME=productid<?=$pid?>        VALUE='<?=$pid?>'>    
        <?
        $candidates = array();
        for($i=1; $i <= DropDownMaxValue; $i++) {
            $candidates[]=$i;
        }

/update我用另一种方法来解决这个问题。只要显示同样的产品就可以了。

function push_barcode() {
// alert('barcode pushing');
b  = document.form1.barcode;
bs = document.form1.barcodes;
if (bs.value == "") {
    bs.value = b.value;
} else { // ?? 111,333,444,... ???
    bs.value = b.value;
}

}

php arrays sorting sql-order-by
1个回答
0
投票

好的,你应该可以用这个方法来获得计数。keys 返回的值与数组的括号中的值相反。

试试下面的方法,看看是否适合你...

$ProductName = $ary[0]['ProductName'];
//--> get the count of your array
$count = count($ProductName);
//--> define an output variable to hold values
$output = null;
$i = 0;
//--> reduce count by one as key values start at 0 and count starts at 1
$count = $count - 1;
//--> subtraction and addition of $count by one to reduce warning 
//--> of `undefined offset` for index on mismatched key values
while($count + 1){
        //--> concatenate the values into a variable for display
        $output .= $ProductName[$count];
        //--> or push the reversed values back into an array 
        $product_name[$i] = $ProductName[$count];
        //--> $product_name should now hold your values in reverse use a foreach 
        // use a foreach to display values in your table
        $count--;
        $i++;
}

© www.soinside.com 2019 - 2024. All rights reserved.