Sorting by Year (Finding Min and Max Range) in Multidimensional Year-Make-Model PHP Array

Sorting by Year (Finding Min and Max Range) in Multidimensional Year-Make-Model PHP Array



Here's a tiny look at my original array


Array
(
[0] => Array
(
[0] => 1993
[1] => Jeep
[2] => Cherokee
[3] => Base Sport Utility 2-Door
[4] => 4.0L 242Cu. In. l6 GAS OHV Naturally Aspirated
)

[1] => Array
(
[0] => 1992
[1] => Jeep
[2] => Cherokee
[3] => Base Sport Utility 2-Door
[4] => 4.0L 242Cu. In. l6 GAS OHV Naturally Aspirated
)

[2] => Array
(
[0] => 1991
[1] => Jeep
[2] => Cherokee
[3] => Base Sport Utility 2-Door
[4] => 4.0L 242Cu. In. l6 GAS OHV Naturally Aspirated
)



So, let's have some fun, or bear through some pain, and loop this.



First, let's make a bunch of pointless arrays that you aren't going to use. Maybe make a counter for the loop.


$yearsarray = array();
$makearray = array();
$modelarray = array();
$fullarray = array();
$lowestyear = 0;
$highestyear = 9999;
$count = 0;



Okay, cool. Now the fun part, the loop.



Here's what I have at the current moment


foreach ($ymmarr as $ok)

$yearmakemodel = $ok[0] . ' ' . $ok[1] . ' ' . $ok[2];
$makemodel = $ok[1] . ' ' . $ok[2];

$fullarray[$makemodel] = $yearmakemodel;
$count++;



I tried a bunch of other things too. Tried to (as seen in the variables before the loop) try to make a higher or lower while still trying to hinge on the key (Make and Model).. but failed. The multidimensionality to the array is what I think got me.



Everything I found seems to be related to some sort of usort function binding to the key.. I really can't find anything on an indexed array like below. Maybe I'm missing the obvious.



Anyway, this, admittedly stupid simple loop brings back this


Array
(
[Jeep Cherokee] => Array
(
[0] => 1993 Jeep Cherokee
[1] => 1992 Jeep Cherokee
[2] => 1991 Jeep Cherokee
[3] => 1993 Jeep Cherokee
[4] => 1992 Jeep Cherokee
[5] => 1991 Jeep Cherokee
[6] => 1992 Jeep Cherokee
[7] => 1991 Jeep Cherokee
[8] => 1999 Jeep Cherokee
[9] => 1998 Jeep Cherokee
[10] => 1996 Jeep Cherokee
[11] => 1994 Jeep Cherokee
[12] => 1993 Jeep Cherokee
[13] => 1997 Jeep Cherokee
[14] => 1996 Jeep Cherokee
[15] => 1995 Jeep Cherokee
[16] => 1994 Jeep Cherokee
[17] => 1993 Jeep Cherokee
[18] => 1992 Jeep Cherokee
[19] => 1991 Jeep Cherokee
[20] => 1992 Jeep Cherokee
[21] => 1991 Jeep Cherokee
[22] => 1999 Jeep Cherokee
[23] => 1998 Jeep Cherokee
[24] => 1992 Jeep Cherokee
[25] => 1991 Jeep Cherokee
[26] => 1999 Jeep Cherokee
[27] => 1998 Jeep Cherokee
[28] => 1997 Jeep Cherokee
[29] => 1996 Jeep Cherokee
[30] => 1995 Jeep Cherokee
[31] => 1994 Jeep Cherokee
[32] => 1999 Jeep Cherokee
[33] => 1998 Jeep Cherokee
[34] => 1997 Jeep Cherokee
[35] => 1996 Jeep Cherokee
[36] => 1995 Jeep Cherokee
[37] => 1994 Jeep Cherokee
[38] => 1999 Jeep Cherokee
[39] => 1998 Jeep Cherokee
[40] => 1997 Jeep Cherokee
[41] => 1996 Jeep Cherokee
[42] => 1995 Jeep Cherokee
[43] => 1994 Jeep Cherokee
[44] => 1993 Jeep Cherokee
[45] => 1992 Jeep Cherokee
[46] => 1991 Jeep Cherokee
[47] => 1999 Jeep Cherokee
[48] => 1998 Jeep Cherokee
[49] => 1997 Jeep Cherokee
[50] => 1996 Jeep Cherokee
[51] => 1995 Jeep Cherokee
[52] => 1994 Jeep Cherokee
[53] => 1993 Jeep Cherokee
[54] => 1992 Jeep Cherokee
[55] => 1991 Jeep Cherokee
[56] => 1999 Jeep Cherokee
[57] => 1998 Jeep Cherokee
)

[Jeep Comanche] => Array
(
[0] => 1992 Jeep Comanche
[1] => 1991 Jeep Comanche
[2] => 1992 Jeep Comanche
[3] => 1991 Jeep Comanche
[4] => 1992 Jeep Comanche
[5] => 1991 Jeep Comanche
)

[Jeep Wrangler] => Array
(
[0] => 1993 Jeep Wrangler
[1] => 1992 Jeep Wrangler
[2] => 1991 Jeep Wrangler
[3] => 1992 Jeep Wrangler
[4] => 1991 Jeep Wrangler
[5] => 1994 Jeep Wrangler
[6] => 1993 Jeep Wrangler
[7] => 1992 Jeep Wrangler
[8] => 1991 Jeep Wrangler
[9] => 1995 Jeep Wrangler
[10] => 1994 Jeep Wrangler
[11] => 1999 Jeep Wrangler
[12] => 1998 Jeep Wrangler
[13] => 1997 Jeep Wrangler
[14] => 1995 Jeep Wrangler
[15] => 1994 Jeep Wrangler
[16] => 1993 Jeep Wrangler
[17] => 1992 Jeep Wrangler
[18] => 1991 Jeep Wrangler
[19] => 1999 Jeep Wrangler
[20] => 1998 Jeep Wrangler
[21] => 1999 Jeep Wrangler
[22] => 1998 Jeep Wrangler
[23] => 1997 Jeep Wrangler
)

)



My issue here is I don't know the array keys. They are dynamic. It seems like it would be so simple if instead of [Jeep Cherokee] => Array it would just be like [0] => Array for all the Jeep Cherokees, [1] => Array for the Comanches, etc.


[Jeep Cherokee] => Array


[0] => Array


[1] => Array



I attempted to do something like this, by declaring these


$next = next($ymmarr);
$nextyear = $next[0];
$nextmakemodel = $next[1] . ' ' . $next[2];



and doing a conditional statement


if ($makemodel == $nextmakemodel)
$fullarray[$count++] = $yearmakemodel;



But no, my logic is apparently flawed.



I've been stuck at this. I know there's an easier way.



One thing I'm trying is creating a flat indexed loop like this


$fullarray2 = $yearmakemodel;



then sort that by year


function natorder($a,$b)
return strnatcmp ( $a, $b );


uasort ($fullarray2, 'natorder');

$fullarray2 = array_values(array_unique($fullarray2));



This brings back something that looks like I can loop over and combine the Makes and Models again, but is this necessary?


Array
(
[0] => 1991 Jeep Cherokee
[1] => 1991 Jeep Comanche
[2] => 1991 Jeep Wrangler
[3] => 1992 Jeep Cherokee
[4] => 1992 Jeep Comanche
[5] => 1992 Jeep Wrangler
[6] => 1993 Jeep Cherokee
[7] => 1993 Jeep Wrangler
[8] => 1994 Jeep Cherokee
[9] => 1994 Jeep Wrangler
[10] => 1995 Jeep Cherokee
[11] => 1995 Jeep Wrangler
[12] => 1996 Jeep Cherokee
[13] => 1997 Jeep Cherokee
[14] => 1997 Jeep Wrangler
[15] => 1998 Jeep Cherokee
[16] => 1998 Jeep Wrangler
[17] => 1999 Jeep Cherokee
[18] => 1999 Jeep Wrangler
)



Honestly all I'm looking to get out of this is basically an array that gives me data like this


Array
(
[0] => 1991 - 1999 Jeep Cherokee
[1] => 1991 - 1992 Jeep Comanche
[2] => 1991 - 1999 Jeep Wrangler
)



Or.. whatever.. something like that that I can easily iterate over.



Am I making this much too complicated?






Hi, I feel your pain from all the way over here, but in your effort to show us you are not asking us to just write the code for you, and have tried lots of things, I for one am now not at all sure what you are trying to achieve. The What I want array does not seem to relate to the what I started with array. Can you try and group the what I got and What I want in some way we can see the logic a bit better please

– RiggsFolly
Sep 13 '18 at 23:02






Your in luck, just use $fullarray = array_values($fullarray) Which will effectively remove those mean associative keys.

– ArtisticPhoenix
Sep 13 '18 at 23:03



$fullarray = array_values($fullarray)






@RiggsFolly it's quite the mash-up I know, that was my experience with it. Knew what I wanted, a min and max year range (however the array was constructed was.. important, but secondary to it's outcome).. I get you though, and got some great answers

– Brian Bruman
Sep 14 '18 at 0:56






@ArtisticPhoenix Ah. I'll remember that one for the future. I tried array_values on another array attempt but not on the original. Tested it out and.. yeah, seperates and indexes, would have been helpful :). Solution there somewhere. Thank you

– Brian Bruman
Sep 14 '18 at 0:58





2 Answers
2



After your code:


foreach ($ymmarr as $ok)

$yearmakemodel = $ok[0] . ' ' . $ok[1] . ' ' . $ok[2];
$makemodel = $ok[1] . ' ' . $ok[2];

$fullarray[$makemodel] = $yearmakemodel;
$count++;



which I would like to simplify to(it is just a detail you can keep your code as it is):


foreach ($ymmarr as $ok)
$fullarray[$ok[1] . ' ' . $ok[2]] = $ok[0] . ' ' . $ok[1] . ' ' . $ok[2];



which give you something like this:


$fullarray= Array
(
'Jeep Cherokee' => Array
(
0 => '1993 Jeep Cherokee',
1 => '1992 Jeep Cherokee',
2 => '1991 Jeep Cherokee',
56 => '1999 Jeep Cherokee'

),

'Jeep Comanche' => Array
(
0 => '1992 Jeep Comanche',
1 => '1991 Jeep Comanche',
2 => '1992 Jeep Comanche',

),

'Jeep Wrangler' => Array
(
0 => '1993 Jeep Wrangler',
1 => '1992 Jeep Wrangler',
2 => '1991 Jeep Wrangler',
11 => '1999 Jeep Wrangler'

)

);



you can directly use array_map to get the expected result.


array_map


$fullarray=array_map(function($val)$end=($end=explode(' ',current($val)))?$end[1].' '.$end[2]:'';$min=intval(min($val));$max=intval(max($val)); return $min.'-'.$max.' '.$end;,array_values($fullarray));



At this step


var_dump($fullarray)



output:


array(3)
[0]=>
string(23) "1991-1999 Jeep Cherokee"
[1]=>
string(23) "1991-1992 Jeep Comanche"
[2]=>
string(23) "1991-1999 Jeep Wrangler"






Wow. A one-line solution to all my woes. Brilliant, really. Logically... it makes sense, looking at it, even if a few things are a bit beyond my current reach. Obviously array_map should be my next PHP study. The fact that you can pierce that all together still blows my mind and obviously had the foresight for the proper outcome. Thank you so much.

– Brian Bruman
Sep 14 '18 at 0:53




If I understand your question correctly, this should do what you're trying to achieve.



For brevity, I have omitted indexes 3 and 4 from your original array.


3


4



Here's a working example: https://3v4l.org/BAter


<?php

$vehicles = [
[1993, 'Jeep', 'Cherokee'],
[1991, 'Jeep', 'Wrangler'],
[1992, 'Jeep', 'Comanche'],
[1994, 'Jeep', 'Cherokee'],
[1992, 'Jeep', 'Wrangler'],
[1993, 'Jeep', 'Cherokee'],
[1996, 'Jeep', 'Comanche'],
[1997, 'Jeep', 'Cherokee'],
[1998, 'Jeep', 'Cherokee'],
[1995, 'Jeep', 'Comanche'],
[1999, 'Jeep', 'Comanche'],
[1998, 'Jeep', 'Wrangler'],
];

$aggregated = ;

foreach ($vehicles as list ($year, $make, $model))
$key = $make . $model;

if (isset($aggregated[$key]) && $year < $aggregated[$key]['min'])
$aggregated[$key]['min'] = $year;
else if (isset($aggregated[$key]) && $year > $aggregated[$key]['max'])
$aggregated[$key]['max'] = $year;
else
$aggregated[$key] = [
'make' => $make,
'model' => $model,
'min' => $year,
'max' => $year,
];



$aggregated = array_map(function ($vehicle)
extract($vehicle);

return ($min === $max)
? "$min: $make $model"
: "$min-$max: $make $model";
, $aggregated);

var_dump(array_values($aggregated));






Interesting solution. Thought about possibly using list or extract.. and array_map's power never ceases to amaze me.. something I need to wrap my head around more to save all these headaches in transforming arrays. This is assuming that my original array looks like this though: Array ( [0] => Array ( [0] => 1993 [1] => Jeep [2] => Cherokee ) [1] => Array ( [0] => 1991 [1] => Jeep [2] => Wrangler ) ) though.. Still very good, and thank you

– Brian Bruman
Sep 14 '18 at 0:36



list


extract


array_map


Array ( [0] => Array ( [0] => 1993 [1] => Jeep [2] => Cherokee ) [1] => Array ( [0] => 1991 [1] => Jeep [2] => Wrangler ) )






@BrianBruman the first array can have exactly the same structure as the first array in your question. I just didn’t want to waste time creating dummy data that wasn’t necessary to answer your question.

– fubar
Sep 14 '18 at 1:24



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)