简体   繁体   English

在php数组中排序唯一值

[英]sorting unique values in a php array

I originally posted this Other Question and have expanded my question and moved onto this question from it. 我最初发布了这个“ 其他问题” ,并扩展了我的问题,然后从中移到了这个问题。

I'm trying to split up an array and remove some partial unique values from a MYSQL SELECT QUERY with PHP . 我正在尝试拆分数组,并使用PHPMYSQL SELECT QUERY删除一些部分唯一的值。

Here's the out put I'm getting... 这是我得到的输出...

Array ( [Order_ID] => 1 [Customer_ID] => 42534 [OrderDate] => [lotCode] => 401042 [qty] => 8 [0] => 
Array ( [0] => 1 [Order_ID] => 1 [1] => 42534 [Customer_ID] => 42534 [2] => [OrderDate] => [3] => 993647 [lotCode] => 993647 [4] => 9 [qty] => 9 ) [1] => 
Array ( [0] => 1 [Order_ID] => 1 [1] => 42534 [Customer_ID] => 42534 [2] => [OrderDate] => [3] => 401040 [lotCode] => 401040 [4] => 55 [qty] => 55 ) [2] => 
Array ( [0] => 1 [Order_ID] => 1 [1] => 42534 [Customer_ID] => 42534 [2] => [OrderDate] => [3] => 401040 [lotCode] => 401040 [4] => 66 [qty] => 66 ) [3] => 
Array ( [0] => 1 [Order_ID] => 1 [1] => 42534 [Customer_ID] => 42534 [2] => [OrderDate] => [3] => 401042 [lotCode] => 401042 [4] => 8 [qty] => 8 ) ) 

Array ( [Order_ID] => 7 [Customer_ID] => 42534 [OrderDate] => [0] => 
Array ( [0] => 2 [Order_ID] => 2 [1] => 42534 [Customer_ID] => 42534 [2] => [OrderDate] => [3] => [lotCode] => [4] => [qty] => ) [1] => 
Array ( [0] => 3 [Order_ID] => 3 [1] => 42534 [Customer_ID] => 42534 [2] => [OrderDate] => [3] => [lotCode] => [4] => [qty] => ) [2] => 
Array ( [0] => 4 [Order_ID] => 4 [1] => 42534 [Customer_ID] => 42534 [2] => [OrderDate] => [3] => [lotCode] => [4] => [qty] => ) [3] => 
Array ( [0] => 5 [Order_ID] => 5 [1] => 42534 [Customer_ID] => 42534 [2] => [OrderDate] => [3] => [lotCode] => [4] => [qty] => ) [4] => 
Array ( [0] => 6 [Order_ID] => 6 [1] => 42534 [Customer_ID] => 42534 [2] => [OrderDate] => [3] => [lotCode] => [4] => [qty] => ) [5] => 
Array ( [0] => 7 [Order_ID] => 7 [1] => 42534 [Customer_ID] => 42534 [2] => [OrderDate] => [3] => [lotCode] => [4] => [qty] => ) )

From this Query... 从此查询...

<?php

$withLotCodes = array();
$noLotCodes = array();

$OrdersToShip2 = mysql_query("
        SELECT o.Order_ID, o.Customer_ID, o.OrderDate, olc.lotCode, olc.qty
        FROM Orders o
        LEFT JOIN OrdersLotCodes olc ON o.Order_ID = olc.Order_ID
        WHERE o.LotCoded = 0 ORDER BY o.Order_ID");

if($OrdersToShip2) {

    while ($info = mysql_fetch_array($OrdersToShip2))
    {
            if(!isset($info['lotCode'])){
                // if NO lotCode
                $noLotCodes['Order_ID']     = $info['Order_ID'];
                $noLotCodes['Customer_ID']  = $info['Customer_ID'];
                $noLotCodes['OrderDate']    = $info['OrderDate'];

                array_push($noLotCodes,$info);

                }else{
                // if YES lotCode
                $withLotCodes['Order_ID']    = $info['Order_ID'];
                $withLotCodes['Customer_ID'] = $info['Customer_ID'];
                $withLotCodes['OrderDate']   = $info['OrderDate'];
                $withLotCodes['lotCode']     = $info['lotCode'];
                $withLotCodes['qty']         = $info['qty'];

                array_push($withLotCodes,$info);

                }
    }
            }else{
            echo "encountered an error.".mysql_error();
    } 

print_r($withLotCodes); 
echo '<br><br>';
print_r($noLotCodes);   

mysql_close($conn);
?>

As you see, it breaks the array up into orders that have 'lotCodes' and orders that do not so I now have two arrays . 如您所见,它将array拆分成具有'lotCodes'订单和没有'lotCodes'订单,因此我现在有两个arrays

Now, I'm trying to remove the duplicate values for the 'Order_ID' 'Customer_ID & 'OrderDate' that have the 'lotCode' values in the first array . 现在,我想删除重复值的'Order_ID' 'Customer_ID'OrderDate'是有'lotCode'率先在值array I only want them to appear once followed by the lotCode values as many as they may be. 我只希望它们出现一次,后跟lotCode值尽可能多的值。

I tried using array_unique but that removed the rest with the lotCodes that I need for the output. 我尝试使用array_unique但是删除了其余的与我需要用于输出的lotCodes

So, what I'm going for would be something like: 所以,我要的是这样的:

Order_ID, Customer_ID, OrderDate, lotCode, qty, lotCode, qty, lotCode, qty, lotCode, qty

Instead of: 代替:

Order_ID, Customer_ID, OrderDate, lotCode, qty
Order_ID, Customer_ID, OrderDate, lotCode, qty
Order_ID, Customer_ID, OrderDate, lotCode, qty
Order_ID, Customer_ID, OrderDate, lotCode, qty

I hope that's clear. 我希望这很清楚。

Any suggestions on this guys? 对这个家伙有什么建议吗?

In terms of your actual code, you could do something like this 根据您的实际代码,您可以执行以下操作

while ($info = mysql_fetch_array($OrdersToShip2))
{
    if(!isset($info['lotCode'])){
        if( !isset( $noLotCodes[$info['Order_ID']]) ) {
            $noLotCodes[$info['Order_ID']] = $info;
        }
        $noLotCodes[$info['Order_ID']]['lotCode'][$info['lotCode']] = $info['qty'];

    }else{
        if( !isset( $withLotCodes[$info['Order_ID']]) ) {
            $withLotCodes[$info['Order_ID']] = $info;
        }
        $withLotCodes[$info['Order_ID']]['lotCode'][$info['lotCode']] = $info['qty'];

    }
}

What this will give you is a multi-dimensional array with the order "header" info (order_id, customer_id and order_date etc) at the top level and then a sub-array of lotCodes and qty. 这将为您提供一个多维数组,该数组具有位于顶层的订单“标头”信息(order_id,customer_id和order_date等),然后是lotCodes和qty的子数组。

But as a more general point you should move away from using the mysql_ family of functions - they're deprecated. 但是,更笼统地说,您应该远离使用mysql_系列函数-不推荐使用它们。 You should use mysqli_ or even better PDO. 您应该使用mysqli_甚至更好的PDO。

Why do you not makes things easier (from my point of view) by creating an array like 为什么不通过创建一个数组来简化事情(从我的角度来看)

// you create your order array
$orders = array();
// Then for each lot and quantity
$order[$Order_ID][$Customer_ID][$OrderDate][] = array( 'lotCode'=>$lotcode, 'qty'=>$qty );
$order[$Order_ID][$Customer_ID][$OrderDate][] = array( 'lotCode'=>$lotcode2, 'qty'=>$qty2 );

// You can test if a specified value exist with isset
// Like
if ( isset (  $order[1][42534 ][3] ) )
{
   // Found order list with order ID 1, cutomer id 42534 and order date 3
}

Edit: 编辑:

// Select emulation
$mysqlreturnarrayemul = array();
$mysqlreturnarrayemul[0]['Order_ID'] = 1;
$mysqlreturnarrayemul[0]['Customer_ID'] = 42534;
$mysqlreturnarrayemul[0]['OrderDate'] = 3;
$mysqlreturnarrayemul[0]['lotCode'] = 1;
$mysqlreturnarrayemul[0]['qty'] = 200;

$mysqlreturnarrayemul[1]['Order_ID'] = 1;
$mysqlreturnarrayemul[1]['Customer_ID'] = 42534;
$mysqlreturnarrayemul[1]['OrderDate'] = 3;
$mysqlreturnarrayemul[1]['lotCode'] = 5;
$mysqlreturnarrayemul[1]['qty'] = 8456;

// you create your order array
$orders = array();
foreach ( $mysqlreturnarrayemul as $info )
{
    // Then you add lot code and quantity
    $order[$info['Order_ID']][$info['Customer_ID']][$info['OrderDate']][] = $info['lotCode'];
    $order[$info['Order_ID']][$info['Customer_ID']][$info['OrderDate']][] = $info['qty'];
}

// You can test if a specified value exist with isset
// Like
if ( isset (  $order[1][42534 ][3] ) )
{
   // Found order list with order ID 1, cutomer id 42534 and order date 3
   print_r($order[1][42534 ][3]);

}
// print_r: Array ( [0] => 1 [1] => 200 [2] => 5 [3] => 8456 ) 

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM