PHP 关联数组排序

发布于 2024-08-25 18:48:55 字数 1119 浏览 2 评论 0原文

这是我的数组,如何按 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] => 
                        )

Here's my array, how do I sort it by 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] => 
                        )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

停滞 2024-09-01 18:48:55
function cmp($a, $b) {
    if ($a['saleref'] == $b['saleref']) {
        return 0;
    }
    return ($a['saleref'] < $b['saleref']) ? -1 : 1;
}

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

uasort($array['xml']['sale'], 'cmp');
殤城〤 2024-09-01 18:48:55

如果您需要维护索引关联,请使用uasort()

否则 usort() 可以工作

示例代码,从手动注释中提取并进行了调整:

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

If you need to maintain index association use uasort().

Otherwise usort() would work

Example code, lifted from manual comments and tweaked:

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'); 
感情废物 2024-09-01 18:48:55

例如,通过使用 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
      )
    )
  )
);
}

E.g. by using uasort()

example script:

$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
      )
    )
  )
);
}
始终不够 2024-09-01 18:48:55
<?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);
?>
<?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);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文