2012-03-27 46 views
1

我最初发布了这个Other Question,并且扩展了我的问题并从它移到了这个问题上。在一个php数组中排序唯一值

我想分割一个数组,并从PHP删除MYSQL SELECT QUERY的一些部分唯一值。

这里是出把我越来越...

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'有第一array'lotCode'值。我只希望它们出现一次,其次是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 

我希望是十分明显的。

对这家伙有什么建议吗?

回答

0

在您的实际代码方面,你可以做这样的事情

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']; 

    } 
} 

什么这会给你是一个多维数组与秩序“头”信息(ORDER_ID,CUSTOMER_ID和order_date的等)在顶层,然后是lotCodes和qty的子数组。

但作为一个更一般的观点,您应该放弃使用mysql_系列函数 - 它们已被弃用。你应该使用mysqli_或更好的PDO。

+0

我想要做的获取每个'lotCode'&'qty'的值,它们是'Order_ID'的一部分,而不会多次获得'Order_ID'。我无法弄清楚它的正确说法。 – Monty 2012-03-28 21:56:25

+0

@Monty好吧,我已经调整了我的代码来提供这个,并添加了一点点解释。 – liquorvicar 2012-03-29 05:41:31

0

你为什么不通过创建像一个阵列,使事情变得更容易(从我的角度来看)

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

问候

+0

请参阅上面的注释。 – Monty 2012-03-28 22:01:41

+0

我更新了我的帖子,向您展示了一种简单的方法来在数组中添加值。你只需要获得Order_iD一次(连同客户id或者orer的日期) – grifos 2012-03-28 23:45:07