Forum Moderators: coopster

Message Too Old, No Replies

Tricky array sorting - sort by specific array key/value?

         

JAB Creations

10:27 pm on Dec 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm working on the operating system bit of my statistics script and I'm not sure how to sort my array.

Here is what the array looks like in general...

$os
$os['Windows']
$os['Windows']['5.0']
$os['Windows']['5.1']
$os['Windows']['5.2']
$os['Windows']['6.0']
$os['OSX']
$os['OSX']['10.4']
$os['OSX']['10.5']
$os['OSX']['10.6']
$os['Linux']
$os['Linux']['Ubuntu']
$os['Linux']['CentOS']
$os['Linux']['Fedora']
$os['Linux']['SUSE']

If I set the count for the arrays like this...

$os['Windows']['count']

...I can sort the arrays by most to least used operating system however I can't store actual versions such as...

$os['Windows']['5.1']

...however if I set the count directly to the array...

$os['Windows']++;

...then PHP refuses to allow other arrays to be stored in that array.

Am I going to have to do something tricky, use two separate arrays (one for counting and one for versions) or am I merely blatantly unaware of something that would really help out a lot? For clarification I have been looking through the comparison of array sorting methods on php.net.

- John

coopster

9:32 pm on Dec 15, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Maybe restructure that $os array ...
$os = array(); 
$os['Windows'] = array();
$os['Windows'][] = array(
'version' => '',
'count' => 0
);
$os['Windows'][] = array(
'version' => '5.0',
'count' => 0
);

Or you may even build the initial index with the version as part of the name ...
$os['Windows'] 
$os['Windows 5.0']

JAB Creations

9:55 pm on Dec 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for your reply coopster. I was trying to figure out if there was a way I could sort arrays in PHP by a specified key though either the wording is really off, it doesn't exist or the wording is spot on and I'm not seeing it? The second suggestion is a good one too however I want to keep the main operating systems together (Windows among Windows) and then have each tbody in the table be dedicated to each OS family (and each tbody naturally ordered top to bottom).

What I've been working on is creating a two array setup using $os1 and $os2 that I'm currently testing and seems to work though I'm certainly still open to suggestions and pushing my understanding of sorting arrays. :)

- John

<?php
if (stristr($user-agent,'Windows NT'))
{
$os1['Windows']++;
$p0 = explode('Windows NT ',$user-agent,2);
$p1 = substr($p0[1],0,3);

if ($p1=='5.1') {$os2['Windows']['Windows XP']++;}
else if ($p1=='5.2') {$os2['Windows']['Windows Server 2003']++;}
else if ($p1=='6.0') {$os2['Windows']['Windows Vista']++;}
else if ($p1=='6.1') {$os2['Windows']['Windows 7']++;}
else {echo '<div>win == '.$useragent.'</div>';}
}

arsort($os1);

foreach ($os1 as $key1 => $value1)
{
echo '<div>1: '.$key1.' == '.$value1.'</div>';

foreach ($os2[$key1] as $key2 => $value2)
{
echo '<div>_2: '.$key2.' == '.$value2.'</div>';

if (is_array($value2))
{
arsort($value2);
foreach ($value2 as $key3 => $value3)
{
echo '<div>__3: '.$key3.' == '.$value3.'</div>';
}
}
}
}
?>

JAB Creations

12:39 am on Dec 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah-ha! Arrays do still mix me up a bit though you can sort by a sub-array is you use similar key names as I have below which at least at the moment seems to be working so I hope this helps someone else out. :)

Instead of having values three arrays deep I'm limiting it to two arrays deep so I won't have Linux -> Ubuntu -> 10.04 have it's own item though instead will have Linux -> Ubuntu 10.04 as it's own item. I can adjust how I interpret each OS version to merge OS X 10.6.1 and 10.6.2 in to simply 10.6 if I'd like. For now it works as desired though I can't blow a more than a few days on a single array sorting problem though I'm still willing to try stuff out if anyone has any further ideas.

- John

foreach ($os1 as $key1 => $value1)
{
arsort($os2[$key1]);
}