按年度到数值对按字母顺序和数字排序数组

时间:2018-03-26 22:24:21

标签: php arrays

我正在查看Array Sorting上的PHP手册,但似乎无法以正确的顺序得到它。

基本上在我的PHP脚本中,我获得了一系列Year Make Models和Engines,然后使用array_unique来获得唯一的Year Make和Models。

Array
(
    [0] => 2012 Jeep Liberty
    [7] => 2011 Jeep Liberty
    [12] => 2010 Jeep Liberty
    [15] => 2009 Jeep Liberty
    [18] => 2008 Jeep Liberty
    [22] => 2007 Jeep Liberty
    [26] => 2006 Jeep Liberty
    [33] => 2006 Jeep Wrangler
    [44] => 2005 Jeep Liberty
    [51] => 2005 Jeep Wrangler
    [60] => 2004 Jeep Liberty
    [64] => 2004 Jeep Wrangler
    [73] => 2003 Jeep Liberty
    [77] => 2003 Jeep Wrangler
    [86] => 2002 Jeep Liberty
    [92] => 2002 Jeep Wrangler
    [96] => 2001 Jeep Cherokee
    [102] => 2001 Jeep Wrangler
    [106] => 2000 Jeep Cherokee
    [114] => 2000 Jeep Wrangler
    [117] => 1999 Jeep Cherokee
    [125] => 1999 Jeep Wrangler
    [128] => 1998 Jeep Cherokee
    [136] => 1998 Jeep Grand Cherokee
    [149] => 1998 Jeep Wrangler
    [152] => 1997 Jeep Cherokee
    [159] => 1997 Jeep Grand Cherokee
    [169] => 1997 Jeep Wrangler
    [173] => 1996 Jeep Cherokee
    [181] => 1996 Jeep Grand Cherokee
    [186] => 1995 Jeep Cherokee
    [193] => 1995 Jeep Grand Cherokee
    [203] => 1995 Jeep Wrangler
)

我想要做的就是对数组进行排序,这样不仅是年份匹配,还有Make和Model。类似的东西:

Array
(
    [0] => 2012 Jeep Liberty
    [7] => 2011 Jeep Liberty
    [12] => 2010 Jeep Liberty
    [15] => 2009 Jeep Liberty
    [18] => 2008 Jeep Liberty
    [22] => 2007 Jeep Liberty
    [26] => 2006 Jeep Liberty
    [44] => 2005 Jeep Liberty
    [60] => 2004 Jeep Liberty
    [73] => 2003 Jeep Liberty
    [86] => 2002 Jeep Liberty
    [33] => 2006 Jeep Wrangler
    [51] => 2005 Jeep Wrangler
    [64] => 2004 Jeep Wrangler
    [77] => 2003 Jeep Wrangler
    [92] => 2002 Jeep Wrangler
    [102] => 2001 Jeep Wrangler 
    [114] => 2000 Jeep Wrangler
    [125] => 1999 Jeep Wrangler
    [149] => 1998 Jeep Wrangler
    [169] => 1997 Jeep Wrangler
    [203] => 1995 Jeep Wrangler
    [96] => 2001 Jeep Cherokee
    [106] => 2000 Jeep Cherokee
    [117] => 1999 Jeep Cherokee
    [128] => 1998 Jeep Cherokee
    [152] => 1997 Jeep Cherokee
    [173] => 1996 Jeep Cherokee
    [186] => 1995 Jeep Cherokee
    [136] => 1998 Jeep Grand Cherokee
    [159] => 1997 Jeep Grand Cherokee
    [181] => 1996 Jeep Grand Cherokee
    [193] => 1995 Jeep Grand Cherokee
)

或者那种性质的东西(手动排序,我可能会对数组进行array_values重新索引键) - 我只想要对Years和Make / Models进行正确排序。

这里我最好的选择是什么?

3 个答案:

答案 0 :(得分:1)

好的,不是100%确定这是你想要的,但是如果你想要一个按引擎排序的数组和按年排序的数组,我可能会为你提供一些不太优雅但有效的解决方案。

首先,为每个引擎名称创建一个新的关联($ res = array())和另一个 2D临时数组(您可以遍历整个数组以获取引擎名称列表,保存您在$ list中已经看到的名称,并在每次遇到新名称时构建临时数组。将该引擎的所有元素放在该数组中,这样就得到了

[0] => 2012 Jeep Liberty
[7] => 2011 Jeep Liberty
[12] => 2010 Jeep Liberty
[15] => 2009 Jeep Liberty
[18] => 2008 Jeep Liberty
[22] => 2007 Jeep Liberty
[26] => 2006 Jeep Liberty
[44] => 2005 Jeep Liberty
[60] => 2004 Jeep Liberty
[73] => 2003 Jeep Liberty
[86] => 2002 Jeep Liberty
例如,

作为$ tempArray ['Jeep Liberty'](如果Jeep Liberty是你选择的第一个名字,因为它的年份最高)。请注意,它已按年份排序。

现在,请记住前面的$ list,它的所有名称都以其外观顺序存储?

foreach($list as $value)
  foreach(tempArray[value] as $key => $value2)
    $res += array($key=>$value2.$value);

最后,$ res的格式为

[0] => 2012 Jeep Liberty
[7] => 2011 Jeep Liberty
[12] => 2010 Jeep Liberty
[15] => 2009 Jeep Liberty
[18] => 2008 Jeep Liberty
[22] => 2007 Jeep Liberty
[26] => 2006 Jeep Liberty
[44] => 2005 Jeep Liberty
[60] => 2004 Jeep Liberty
[73] => 2003 Jeep Liberty
[86] => 2002 Jeep Liberty
[33] => 2006 Jeep Wrangler
[51] => 2005 Jeep Wrangler
[64] => 2004 Jeep Wrangler
[77] => 2003 Jeep Wrangler
[92] => 2002 Jeep Wrangler
[102] => 2001 Jeep Wrangler 
[114] => 2000 Jeep Wrangler
[125] => 1999 Jeep Wrangler
[149] => 1998 Jeep Wrangler
[169] => 1997 Jeep Wrangler
[203] => 1995 Jeep Wrangler
[96] => 2001 Jeep Cherokee
[106] => 2000 Jeep Cherokee
[117] => 1999 Jeep Cherokee
[128] => 1998 Jeep Cherokee
[152] => 1997 Jeep Cherokee
[173] => 1996 Jeep Cherokee
[186] => 1995 Jeep Cherokee
[136] => 1998 Jeep Grand Cherokee
[159] => 1997 Jeep Grand Cherokee
[181] => 1996 Jeep Grand Cherokee
[193] => 1995 Jeep Grand Cherokee

就像你想要的那样。

答案 1 :(得分:1)

我认为这就是你要找的东西:

    <?php
    $data[0] = '2012 Jeep Liberty';
    $data[7] = '2011 Jeep Liberty';
    $data[12] = '2010 Jeep Liberty';
    $data[15] = '2009 Jeep Liberty';
    $data[18] = '2008 Jeep Liberty';
    $data[22] = '2007 Jeep Liberty';
    $data[26] = '2006 Jeep Liberty';
    $data[44] = '2005 Jeep Liberty';
    $data[60] = '2004 Jeep Liberty';
    $data[73] = '2003 Jeep Liberty';
    $data[86] = '2002 Jeep Liberty';
    $data[33] = '2006 Jeep Wrangler';
    $data[51] = '2005 Jeep Wrangler';
    $data[64] = '2004 Jeep Wrangler';
    $data[77] = '2003 Jeep Wrangler';
    $data[92] = '2002 Jeep Wrangler';
    $data[102] = '2001 Jeep Wrangler ';
    $data[114] = '2000 Jeep Wrangler';
    $data[125] = '1999 Jeep Wrangler';
    $data[149] = '1998 Jeep Wrangler';
    $data[169] = '1997 Jeep Wrangler';
    $data[203] = '1995 Jeep Wrangler';
    $data[96] = '2001 Jeep Cherokee';
    $data[106] = '2000 Jeep Cherokee';
    $data[117] = '1999 Jeep Cherokee';
    $data[128] = '1998 Jeep Cherokee';
    $data[152] = '1997 Jeep Cherokee';
    $data[173] = '1996 Jeep Cherokee';
    $data[186] = '1995 Jeep Cherokee';
    $data[136] = '1998 Jeep Grand Cherokee';
    $data[159] = '1997 Jeep Grand Cherokee';
    $data[181] = '1996 Jeep Grand Cherokee';
    $data[193] = '1995 Jeep Grand Cherokee';


    function my_cmp($value_1, $value_2)
    {
        $str_1_parts = explode(' ', $value_1);
        $str_2_parts = explode(' ', $value_2);
        $year_1 = (int)$str_1_parts[0];
        $year_2 = (int)$str_2_parts[0];
        if( $year_1 > $year_2 ) return 1;
        if( $year_1 < $year_2 ) return -1;

        unset( $str_1_parts[0] );
        unset( $str_2_parts[0] );
        return strcasecmp( implode(' ', $str_1_parts) , implode(' ', $str_2_parts));
    }
    usort($data,'my_cmp');

    print_r( $data );

答案 2 :(得分:1)

如果你想先按Maker和Model排序,你可以做这样的事情(假设年份总是有四位数):

<?php
usort(
    $array,
    function ($value1, $value2) {
        $parts1 = explode(' ', $value1, 2);
        $parts2 = explode(' ', $value2, 2);
        return strcasecmp(
            "$parts1[1] $parts1[0]",
            "$parts2[1] $parts2[0]"
        );
    }
);
print_r($array);

诀窍是在比较顺序时重新排列琴弦,例如&#34; Jeep Cherokee 1995&#34;。

输出:

Array
(
    [0] => 1995 Jeep Cherokee
    [1] => 1996 Jeep Cherokee
    [2] => 1997 Jeep Cherokee
    [3] => 1998 Jeep Cherokee
    [4] => 1999 Jeep Cherokee
    [5] => 2000 Jeep Cherokee
    [6] => 2001 Jeep Cherokee
    [7] => 1995 Jeep Grand Cherokee
    [8] => 1996 Jeep Grand Cherokee
    [9] => 1997 Jeep Grand Cherokee
    [10] => 1998 Jeep Grand Cherokee
    [11] => 2002 Jeep Liberty
    [12] => 2003 Jeep Liberty
    [13] => 2004 Jeep Liberty
    [14] => 2005 Jeep Liberty
    [15] => 2006 Jeep Liberty
    [16] => 2007 Jeep Liberty
    [17] => 2008 Jeep Liberty
    [18] => 2009 Jeep Liberty
    [19] => 2010 Jeep Liberty
    [20] => 2011 Jeep Liberty
    [21] => 2012 Jeep Liberty
    [22] => 1995 Jeep Wrangler
    [23] => 1997 Jeep Wrangler
    [24] => 1998 Jeep Wrangler
    [25] => 1999 Jeep Wrangler
    [26] => 2000 Jeep Wrangler
    [27] => 2001 Jeep Wrangler
    [28] => 2002 Jeep Wrangler
    [29] => 2003 Jeep Wrangler
    [30] => 2004 Jeep Wrangler
    [31] => 2005 Jeep Wrangler
    [32] => 2006 Jeep Wrangler
)

这是另一种解决方案(没有先前的假设):

<?php
$enginesYears = array();
foreach ($array as $value) {
    $parts = explode(' ', $value, 2);
    $year = (int)$parts[0];
    $engine = $parts[1];
    if (!isset($enginesYears[$engine])) {
        $enginesYears[$engine] = array();
    }
    $enginesYears[$engine][] = $year;
}
ksort($enginesYears);
$array = array();
foreach ($enginesYears as $engine => $years) {
    rsort($years);
    foreach ($years as $year) {
        $array[] = "$year $engine";
    }
}
print_r($array);

输出:

Array
(
    [0] => 2001 Jeep Cherokee
    [1] => 2000 Jeep Cherokee
    [2] => 1999 Jeep Cherokee
    [3] => 1998 Jeep Cherokee
    [4] => 1997 Jeep Cherokee
    [5] => 1996 Jeep Cherokee
    [6] => 1995 Jeep Cherokee
    [7] => 1998 Jeep Grand Cherokee
    [8] => 1997 Jeep Grand Cherokee
    [9] => 1996 Jeep Grand Cherokee
    [10] => 1995 Jeep Grand Cherokee
    [11] => 2012 Jeep Liberty
    [12] => 2011 Jeep Liberty
    [13] => 2010 Jeep Liberty
    [14] => 2009 Jeep Liberty
    [15] => 2008 Jeep Liberty
    [16] => 2007 Jeep Liberty
    [17] => 2006 Jeep Liberty
    [18] => 2005 Jeep Liberty
    [19] => 2004 Jeep Liberty
    [20] => 2003 Jeep Liberty
    [21] => 2002 Jeep Liberty
    [22] => 2006 Jeep Wrangler
    [23] => 2005 Jeep Wrangler
    [24] => 2004 Jeep Wrangler
    [25] => 2003 Jeep Wrangler
    [26] => 2002 Jeep Wrangler
    [27] => 2001 Jeep Wrangler
    [28] => 2000 Jeep Wrangler
    [29] => 1999 Jeep Wrangler
    [30] => 1998 Jeep Wrangler
    [31] => 1997 Jeep Wrangler
    [32] => 1995 Jeep Wrangler
)

诀窍是先建立这个:

Array
(
    [Jeep Cherokee] => Array
        (
            [0] => 2001
            [1] => 2000
            [2] => 1999
            [3] => 1998
            [4] => 1997
            [5] => 1996
            [6] => 1995
        )

    [Jeep Grand Cherokee] => Array
        (
            [0] => 1998
            [1] => 1997
            [2] => 1996
            [3] => 1995
        )

    [Jeep Liberty] => Array
        (
            [0] => 2012
            [1] => 2011
            [2] => 2010
            [3] => 2009
            [4] => 2008
            [5] => 2007
            [6] => 2006
            [7] => 2005
            [8] => 2004
            [9] => 2003
            [10] => 2002
        )

    [Jeep Wrangler] => Array
        (
            [0] => 2006
            [1] => 2005
            [2] => 2004
            [3] => 2003
            [4] => 2002
            [5] => 2001
            [6] => 2000
            [7] => 1999
            [8] => 1998
            [9] => 1997
            [10] => 1995
        )

)