The easiest way I find to do it is by creating a new array with the number_of_reffers
as the key. Then sort the new array with either ksort()
or krsort()
. This in the end leaves the original array intact. Otherwise creating the original array in the intended format would be better.
<?php
// original array
$array = array(
array('refferrer' => 'http://google.com/', 'number_of_reffers' => '15'),
array('refferrer' => 'https://facebook.com/', 'number_of_reffers' => '22'),
array('refferrer' => 'none', 'number_of_reffers' => '74'),
array('refferrer' => 'http://findy.com/', 'number_of_reffers' => '6')
);
$foo = array(); // new empty array
// loop through $array, assign the number_of_reffers as the key for the refferrer
foreach ($array as $key => $bar) {
$foo[$bar['number_of_reffers']] = $bar['refferrer'];
}
/*
new array will be:
array(
'15' => 'http://google.com/',
'22' => 'https://facebook.com/'
etc .....
)
*/
// use Ksort / Krsort to sort the key asc or desc
ksort($foo); // ascending order
#krsort($foo); // descending order
die('<pre>'.print_r($foo, true).'</pre>'); // pretty printing of sorted array
?>
As a function ....
<?php
Function Referrer_sort($array, $asc = true) {
IF (!is_array($array)) { return 'not an array'; }
$result = array();
foreach ($array as $key => $value) {
$result[$value['number_of_reffers']] = $value['refferrer'];
}
switch ($asc) {
case false: krsort($result); return $result;
default: ksort($result); return $result;
}
}
$foo_asc = Referrer_sort($array);
$foo_desc = Referrer_sort($array, false);
die('<pre>Ascending:<br>'.print_r($foo_asc, true).'<br>Descending:<br>'.print_r($foo_desc, true).'</pre>');
?>
Changing the original array
Modifying the original array by changing the index key with the value of number_of_reffers
.
<?php
Function Rebuild_Referrer_sort($array, $asc = true) {
IF (!is_array($array)) { return 'not an array'; }
$result = array();
foreach ($array as $key => $value) {
$result[$value['number_of_reffers']] = array('refferrer' => $value['refferrer'], 'number_of_reffers' => $value['number_of_reffers']);
}
switch ($asc) {
case false: krsort($result); return $result;
default: ksort($result); return $result;
}
}
$foo_asc = Rebuild_Referrer_sort($array);
$foo_desc = Rebuild_Referrer_sort($array, false);
die('<pre>Ascending:<br>'.print_r($foo_asc, true).'<br>Descending:<br>'.print_r($foo_desc, true).'</pre>');
/**
Returns:
Ascending:
Array
(
[6] => Array
(
[refferrer] => http://findy.com/
[number_of_reffers] => 6
)
[15] => Array
(
[refferrer] => http://google.com/
[number_of_reffers] => 15
)
[22] => Array
(
[refferrer] => https://facebook.com/
[number_of_reffers] => 22
)
[74] => Array
(
[refferrer] => none
[number_of_reffers] => 74
)
)
Descending:
Array
(
[74] => Array
(
[refferrer] => none
[number_of_reffers] => 74
)
[22] => Array
(
[refferrer] => https://facebook.com/
[number_of_reffers] => 22
)
[15] => Array
(
[refferrer] => http://google.com/
[number_of_reffers] => 15
)
[6] => Array
(
[refferrer] => http://findy.com/
[number_of_reffers] => 6
)
)
*/
?>