php - 对php数组中的唯一值进行排序

标签 php mysql

我最初发布了这个 Other Question并扩展了我的问题并从中转移到这个问题。

我正在尝试拆分一个数组并从 MYSQL SELECT QUERY 中删除一些部分唯一值与 PHP .

这是我得到的输出...

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] => ) )

从这个查询...

<?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);
?>

如您所见,它打破了 array进入具有 'lotCodes' 的订单和订单不这样我现在有两个 arrays .

现在,我正在尝试删除 'Order_ID' 的重复值'Customer_ID & 'OrderDate''lotCode'第一个值 array .我只希望它们出现一次,然后是尽可能多的 lotCode 值。

我尝试使用 array_unique但这用 lotCodes 删除了其余部分我需要的输出。

所以,我要做的是:

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

代替:

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

我希望这很清楚。

对这些人有什么建议吗?

最佳答案

你为什么不通过创建一个像这样的数组来让事情变得更容易(从我的角度来看)

// 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
}

编辑:

// 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 ) 

问候

关于php - 对php数组中的唯一值进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9892666/

相关文章:

PHP 按钮,在 php 文件中处理,然后重定向到另一个页面

php - 类型定义字符串中的元素数量与绑定(bind)变量的数量不匹配

php - mysql/php 选择每年的第一条和最后一条记录在数据库的特定列中有记录

php - MYSQL 或 SQL SELECT 与比较(HAVING)和组

PHP 基于最小和最大半径的距离

javascript - 当网址不为空时歌曲无法播放

php - 我是否需要为 AngularJS 的每条内容创建一个单独的页面?

mysql - 我在 R 中编写了一个查询(SQL)来从数据库中获取一些数据。我想传递由 R 中的子例程指定的日期,而不是对其进行硬编码

mysql - 找到正确的主键

php - 如何使用其他表中的外键删除具有主键的行?