PHP Sort($Array) Returning 1 Instead of Sorted Array

Unable to get sort Array to work

If i understand correctly you want to sort the array by ascending values? if this is correct Look at asort() which will sort the array by value but keep the index/key intact

http://php.net/manual/en/function.asort.php

Keep in mind that the sort functions return a Boolean so don't assign them to a variable, just call the function on the array.

asort($array);

Sort a php array returning new array

Here's a one-liner:

call_user_func(function(array $a){asort($a);return $a;}, $some_mixed_array);

php sort() on array produces wrong result

sort() sorts the array in-place. Don't re-assign it.

Correct:

sort($this->pageLinks);

Incorrect:

$this->pageLinks = sort($this->pageLinks);

php sort multidimensional array: bool(true) instead of an sorted array

Your sorted values are inside $aFacetConfig[$oPropertyCategory->name]. The documentation explain that the output is only a boolean and the array is pass by reference.

If you want to declare an other variable this should works :

$aSortedFacets = $aFacetConfig[$oPropertyCategory->name];
usort($aSortedFacets, function($a, $b) {
return $a['values'] <=> $b['values'];
});
var_dump($aSortedFacets);

PHP: Sort an array by the length of its values?

Use http://us2.php.net/manual/en/function.usort.php

with this custom function

function sort($a,$b){
return strlen($b)-strlen($a);
}

usort($array,'sort');

Use uasort if you want to keep the old indexes, use usort if you don't care.

Also, I believe that my version is better because usort is an unstable sort.

$array = array("bbbbb", "dog", "cat", "aaa", "aaaa");
// mine
[0] => bbbbb
[1] => aaaa
[2] => aaa
[3] => cat
[4] => dog

// others
[0] => bbbbb
[1] => aaaa
[2] => dog
[3] => aaa
[4] => cat

sorting array value without using built in php like sort() etc

Here is the way of sorting.

<?php

$array=array('2','4','8','5','1','7','6','9','10','3');

echo "Unsorted array is: ";
echo "<br />";
print_r($array);

for($j = 0; $j < count($array); $j ++) {
for($i = 0; $i < count($array)-1; $i ++){

if($array[$i] > $array[$i+1]) {
$temp = $array[$i+1];
$array[$i+1]=$array[$i];
$array[$i]=$temp;
}
}
}

echo "Sorted Array is: ";
echo "<br />";
print_r($array);

?>

Sort array - that specific values will be first

Another solution; using a custom function to move an element to the beginning of an array

function __unshift(&$array, $value){
$key = array_search($value, $array);
if($key) unset($array[$key]);
array_unshift($array, $value);
return $array;
}

$a = array("Apple", "Orange", "Banana", "Melon");
__unshift($a, "Melon");
__unshift($a, "Orange");
print_r($a);

Output:

Array
(
[0] => Orange
[1] => Melon
[2] => Apple
[3] => Banana
)

Demo

Or you may use the following to reorder an array using another array having reordered index

function __reorder(&$a, &$b){
$c = array();
foreach($b as $index){
array_push($c, $a[$index]);
}
return $c;
}

// the original array
$a = array("Apple", "Orange", "Banana", "Melon");
// an array with reordered index
$b = array(1, 3, 0, 2);
$c = __reorder($a, $b);
print_r($c);

Demo

PHP - Sorting Associative Array with ksort()

Don't put array sort result into array variable

$fruit[2999] = 'apple';
$fruit[332] = 'banana';
$fruit[400] = 'pineapple';
$fruit[87] = 'pear';

ksort($fruit);

print_r($fruit);

You got out 1 because ksort return true/false. and you print this
value.



Related Topics



Leave a reply



Submit