Php 为什么我的排序算法不起作用?

Php 为什么我的排序算法不起作用?,php,algorithm,sorting,Php,Algorithm,Sorting,我正在为一个客户开发一个系统,它创建一个csv的包装标签,然后发送到打印机。客户有六个不同的项目。客户向我的客户批量订购产品。产品A和产品B两项共用同一包装线。为了使包装更有效,我的客户想先在包装产品A和包装产品B之间进行选择 例如,如果John、Sally和James都订购了这两种产品,则系统需要将John的订单从产品A开始写入csv,Sally的订单从产品B开始写入csv,James的订单从产品A开始写入csv 我已经在下面粘贴了我的非工作代码,但这真的让我头疼,我现在很难处理它 forea

我正在为一个客户开发一个系统,它创建一个csv的包装标签,然后发送到打印机。客户有六个不同的项目。客户向我的客户批量订购产品。产品A和产品B两项共用同一包装线。为了使包装更有效,我的客户想先在包装产品A和包装产品B之间进行选择

例如,如果John、Sally和James都订购了这两种产品,则系统需要将John的订单从产品A开始写入csv,Sally的订单从产品B开始写入csv,James的订单从产品A开始写入csv

我已经在下面粘贴了我的非工作代码,但这真的让我头疼,我现在很难处理它

foreach($orders as $order) {

    $name = null;
    $phone = null;

    $account = DAO_ContactPerson::get($order->account_id);
    $delivery = false;
    if($account->is_agency) {
        $name = $order->getAttribute('name');
        $phone = $order->getAttribute('phone');
    } else {
        $name = sprintf("%s %s",
            $account->getPrimaryAddress()->first_name,
            $account->getPrimaryAddress()->last_name
        );
        $phone = $account->phone;
    }

    $name = trim($name);
    $phone = trim($phone);
    $items = $order->getItems();

    if($order->getAttribute('delivery')) {
        $type = 'deliveries';
        $destination = 'Delivery';
        $address = sprintf("%s %s %s",
            $order->getAttribute('delivery_address.line1'),
            $order->getAttribute('delivery_address.line2'),
            $order->getAttribute('delivery_address.postal')
        );
    } else {
        $type = 'pickups';
        $agency = DAO_ContactPerson::getAgency($order->getAttribute('pickup'));
        $destination = $agency->name;

        // Override account id so orders are grouped by agency
        $order->account_id = $agency->id;
        $address = null;
    }
//          var_dump($order->id);
    // Init account array
    if(!isset($rows[$type][$order->account_id]))
        $rows[$type][$order->account_id] = array('combined' => array(), 'separate' => array());

    foreach($items as $item) {
        $packing = 'separated';
        if($item->product_id == 3 || $item->product_id == 4)
            $packing = 'combined';

        if(!isset($rows[$type][$order->account_id][$packing][$item->product_id]))
            $rows[$type][$order->account_id][$packing][$item->product_id] = array();
        $i = 0;
        while($i < $item->quantity) {
            $rows[$type][$order->account_id][$packing][$item->product_id][] = array(
                    'number' => $order->id,
                    'destination' => $destination,
                    'size' => $item->product_id,
                    'name' => $name,
                    'address' => $address,
                    'phone' => $phone
                );
            $i++;
        }

    }
//          if($order->id == 176) {
//              var_dump($rows[$type][$order->account_id][$packing]);
//          }
}

$this->weight = 1;

$pickups = count($rows['pickups']);
for($i = 0; $i < $pickups; $i++) {
    $account =& $rows['pickups'][$i];
    $account['output'] = array();
    if(isset($account['combined'])) {
        $combined_products =& $account['combined'];
        if(!empty($combined_products)) {
            foreach($combined_products as $prod_id => $combined) {
                usort($combined_products[$prod_id], array($this, "_compareBoxes"));
            }
            // Flip weights once we finish with this account
            $last_box = end($combined_products);
            $last_box = array_pop($last_box);
            reset($combined_products);
            if($this->weight == 1) {
                $this->weight = -1;
                if($last_box['size'] == 3) {
                    asort($combined_products);
                }
            } else {
                if($last_box['size'] == 4) {
                    arsort($combined_products);
                }
                $this->weight = 1;
            }
            foreach($combined_products as $combined) {
                $account['output'][] = $combined;
            }
            foreach($account['separated'] as $separate) {
                $account['output'][] = $separate;
            }
        }
    } else {
        if(isset($account['separated']))
            $account['output'] = $account['separated'];
    }
}

$deliveries = count($rows['deliveries']);
for($i = 0; $i < $deliveries; $i++) {
    $account =& $rows['deliveries'][$i];
    $account['output'] = array();
    if(isset($account['combined'])) {
        $combined_products =& $account['combined'];
        if(!empty($combined_products)) {
            foreach($combined_products as $prod_id => $combined) {
                usort($combined_products[$prod_id], array($this, "_compareBoxes"));
            }
            // Flip weights once we finish with this account
            $last_box = end($combined_products);
            $last_box = array_pop($last_box);
            reset($combined_products);
            if($this->weight == 1) {
                $this->weight = -1;
                if($last_box['size'] == 3) {
                    asort($combined_products);
                }
            } else {
                if($last_box['size'] == 4) {
                    arsort($combined_products);
                }
                $this->weight = 1;
            }
            foreach($combined_products as $combined) {
                $account['output'][] = $combined;
            }
            foreach($account['separated'] as $separate) {
                $account['output'][] = $separate;
            }
        }
    } else {
        if(isset($account['separated']))
            $account['output'] = $account['separated'];
    }
}

$rows['output'] = $rows['pickups'];
array_push($rows['output'], $rows['deliveries']);

$output = '';
foreach($rows['output'] as $account_id => $boxes) {
    if(!empty($boxes['output'])) {
        foreach($boxes['output'] as $labels) {
            if(!empty($labels)) {
                foreach($labels as $label) {
                    $output .= implode(',', $label) . "<br>";
                }
            }
        }
    }
}
_compareBoxes方法如下所示:

private function _compareBoxes($a, $b) {
    if($a['size'] == $b['size']) {
        return 0;
    }
    if($this->weight == 1) {
        // Medium first, then Large
        return ($a['size'] < $b['size']) ? -1 : 1;
    }
    if($this->weight == -1) {
        // Large first, then Medium
        return ($a['size'] > $b['size']) ? -1 : 1;
    }
}

$account=&$rows['pickups'][$i];这是故意的吗?问题是什么?数组没有相应地排序?错误或者?一行中的这两条语句会导致忽略第一条语句:$last_box=end$combined_products;和$last\u box=array\u pop$last\u box;。那不太好。你能为$rows['pickups']提供一些输入数据吗?我为上下文添加了更多代码。$account=&$rows['pickups'][$i]是指定的。它通过引用分配变量,因此当我更改$accounts时,实际上是在更改$rows['pickups'][$I]。这里的问题是它没有被正确地分类。处理完每个帐户后,它需要在从高到低再从低到高的排序之间切换。我已经添加了更多的代码,尽管我真的不确定它是否会有任何帮助。将从数据库中读取订购的产品,然后将其添加到阵列中,以便提货或发货。