1

Here is my array:

$arrayA = array(0 => "someString",
                1 => "otherString",
                2 => "2017",
                3 => "anotherString",
                4 => "2016"); 

My goal is to to find the first item that has a numeric value (which would be "2017") and place it first in the array, without changing its original key and keeping the others in the same order.

So I want:

$arrayA = array(2 => "2017",
                0 => "someString",
                1 => "otherString",
                3 => "anotherString",
                4 => "2016"); 

I tried uasort() php function and it seems the way to do it, but I could not figure out how to build the comparison function to go with it.

PHP documentation shows an example:

function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

But, WHO is $a and WHO is $b?

Well, I tried

    function my_sort($a,$b) {
        if ($a == $b ) {
            return 0;
        } 
        if (is_numeric($a) && !is_numeric($b)) {
            return -1;
            break;
        }
    }

But, of course, I am very far from my goal. Any help would be much appreciated.

Luis Rock
  • 197
  • 3
  • 11
  • you better give the keys a name, like `$arrayA['year'] = 2017` so you get `year => 2017` – AXAI Sep 14 '17 at 14:38
  • 1
    → https://stackoverflow.com/q/17364127/476 You want a *stable* `uasort`… – deceze Sep 14 '17 at 14:38
  • Yes you can, use an associative array $arr['2']= 2017; – bjesua Sep 14 '17 at 14:45
  • *"But, WHO is `$a` and WHO is `$b`?"* -- sorting involves comparison of items of the list that is sorted (two items at a time). `$a` and `$b` are two items from your list. When they need to compare `$a` to `$b`, the [`u*sort()`](http://php.net/manual/en/array.sorting.php) sorting functions let the provided callback function decide if they are equal or, if they are not equal, which one is the smaller and which one is the greater. – axiac Sep 14 '17 at 14:49
  • AXAI, I don´t have that option. – Luis Rock Sep 14 '17 at 15:23

1 Answers1

3

You don't need to sort per se. Once you find the element in question, you can simply push it onto the front of the array with the + operator:

foreach ($arrayA as $k => $v) {
    if (is_numeric($v)) {
        $arrayA = [$k => $v] + $arrayA;
        break;
    }
}
print_r($arrayA);

Yields:

Array
(
    [2] => 2017
    [0] => someString
    [1] => otherString
    [3] => anotherString
    [4] => 2016
)
Alex Howansky
  • 44,270
  • 7
  • 68
  • 92
  • 2
    It should be `unset($arrayA[$k]);` but if you use `+` to join the arrays you don't even need to unset it. The [array union operator (`+`)](http://php.net/manual/en/language.operators.array.php) produces an array that contains all the keys (and the corresponding values) from its first operand and the keys (and values) from the second operand that are not present in the first one. – axiac Sep 14 '17 at 14:53
  • Ah dang it, I always forget that's the way it works, thanks, edited. – Alex Howansky Sep 14 '17 at 14:56