【问题标题】:sort associative array PHP排序关联数组 PHP
【发布时间】:2011-01-28 23:21:29
【问题描述】:

这是我的数组,如何按 saleref 排序?

Array
(
    [xml] => Array
        (
            [sale] => Array
                (
                    [0] => Array
                        (
                            [saleref] =>  12345
                            [saleline] =>   1
                            [product] => producta
                            [date] => 19/ 3/10
                            [manifest] =>       0
                            [qty] =>     1
                            [nextday] => 
                            [order_status] => 
                        )

                    [1] => Array
                        (
                            [saleref] =>  12344
                            [saleline] =>   1
                            [product] => productb
                            [date] => 18/ 3/10
                            [manifest] =>   11892
                            [qty] =>     1
                            [nextday] => 
                            [order_status] => 
                        )

【问题讨论】:

标签: php arrays associative


【解决方案1】:
function cmp($a, $b) {
    if ($a['saleref'] == $b['saleref']) {
        return 0;
    }
    return ($a['saleref'] < $b['saleref']) ? -1 : 1;
}

uasort($array['xml']['sale'], 'cmp');

【讨论】:

    【解决方案2】:

    如果您需要maintain index association,请使用uasort()

    否则usort()would work

    示例代码,取自手动 cmets 并进行了调整:

    function sortSalesRef($a, $b) {
    
        $a = $a['saleref'];
        $b = $b['saleref'];
    
        if ($a == $b) {
            return 0;
        }
    
        return ($a < $b) ? -1 : 1;
    
    }
    
    usort($xml['xml']['sale'], 'sortSalesRef'); 
    

    【讨论】:

      【解决方案3】:

      例如通过使用uasort()

      示例脚本:

      $data = getData();
      uasort($data['xml']['sale'], function($a, $b) {  return strcasecmp($a['saleref'], $b['saleref']); });
      print_r($data);
      
      function getData() {
      return array(
        'xml' => array(
          'sale' => array (
            array(
              'saleref' => '12345',
              'saleline' => 1,
              'product' => 'producta',
              'date' => '19/ 3/10',
              'manifest' => 0,
              'qty' => 1,
              'nextday' => false,
              'order_status' => false
            ),
      
            array(
              'saleref' => '12344',
              'saleline' => 1,
              'product' => 'productb',
              'date' => '18/ 3/10',
              'manifest' => 11892,
              'qty' => 1,
              'nextday' => false,
              'order_status' => false
            )
          )
        )
      );
      }
      

      【讨论】:

        【解决方案4】:
        <?php
        // Comparison function
        function cmp($a, $b)
        {
            if ($a == $b)
                {
                    return 0;
                }
             return ($a < $b) ? -1 : 1;
        }
        $array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
        print_r($array);
        uasort($array, 'cmp');
        print_r($array);
        ?>
        

        【讨论】:

          猜你喜欢
          • 2011-02-26
          • 1970-01-01
          • 2012-08-25
          • 1970-01-01
          • 1970-01-01
          • 2011-06-21
          • 1970-01-01
          • 2016-12-28
          相关资源
          最近更新 更多