1

according to this answer -> Sort an Array by keys based on another Array?, I use this function to get my another array sorted:

function sortArrayByArray($array,$orderArray) {
    $ordered = array();
    foreach($orderArray as $key) {
        if(array_key_exists($key,$array)) {
            $ordered[$key] = $array[$key];
            unset($array[$key]);
        }
    }
    return $ordered + $array;
}

at first, I have my code like this and it works fine

$array1 = array("a","b","c");
$array2 = array("2","5","1");
$array3 = array("2","5","1");
rsort($array3); //5,2,1

for($i=0;$i<3;$i++){
    $customer1[$array2[$i]] = $array1[$i];
}

$properOrderedArray1 = sortArrayByArray($customer1, $array3);

print_r($properOrderedArray1);

but when I use some logic math like multiply, it gets any errors like it said there is data type float

//multiply
$a = 100000*100000;
$b = 200000*200000;
$c = 300000*300000;

$array1 = array("a","b","c");
$array2 = array($a,$b,$c);
$array3 = array($a,$b,$c);
rsort($array3); //5,2,1

for($i=0;$i<3;$i++){
    $customer1[$array2[$i]] = $array1[$i];
}

$properOrderedArray1 = sortArrayByArray($customer1, $array3);

print_r($properOrderedArray1);

var_dump($array2);

THE ERROR: Warning: array_key_exists(): The first argument should be either a string or an integer

so any solution for this problem? Thanks.

Community
  • 1
  • 1
  • 1
    not sure about it, but 100000*100000 is way to big for an integer. therefore it is treated as float and that isn´t allowed as index. – luk2302 Jun 27 '13 at 17:56
  • but the main thing is, how to keep using the function if the the data type is float? or you have any functions to solve this problem? –  Jun 27 '13 at 17:57
  • @PeterPivarc - can you expain your comment with code here? :) –  Jun 27 '13 at 18:08

2 Answers2

1

As stated, you will have to convert your floats to strings. I guess you can alter your function to something like this to make it work:

function sortArrayByArray($array,$orderArray) {
    $ordered = array();
    foreach($orderArray as &$key) {
        if (!is_int($key))
            $key = number_format($key,0,'.','');
        if(array_key_exists($key,$array)) {
            $ordered[$key] = $array[$key];
            unset($array[$key]);
        }
    }
    return $ordered + $array;
}

The problem about doing it this way is that you lose precision in your float value. In a 64-bit system all these values

$a = 9223372036854775808;
$b = 9223372036854775809;
$c = 9223372036854775810;

will be converted into the same float(9.2233720368548E+18), and converting it to a string will give string(19) "9223372036854775808". As long as your indexes that you use for sorting have significant numbers in the upper range of the number it can work, but it's not a safe way of sorting.

bvx89
  • 868
  • 5
  • 12
0

Basically what i written in the comment is true as i just checked in the manual: http://php.net/manual/en/language.types.integer.php

$large_number = 2147483647; //treated as int
$large_number = 2147483648; //treated as float since it is too large for an integer

So the solution to your problem is to limit the indices to the max value of integers. OR to convert the floats to strings, but i would NOT recommend that, but sadly that seems to be the only way to achieve what you are trying to do.

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • I can't because it's according to the company's data, it has a lot of data with big numbers –  Jun 27 '13 at 18:01
  • then you have a problem and as far as i can see there is NO way to numeric indices, then you would have to use strings as keys as they are not limited in size/length. – luk2302 Jun 27 '13 at 18:04