简体   繁体   English

在php中基于另一个数组对数组进行排序?

[英]Sort an array based on another array in php?

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 但是当我使用诸如乘法之类的逻辑数学时,它会得到任何错误,就像说数据类型为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 错误:警告:array_key_exists():第一个参数应为字符串或整数

so any solution for this problem? 那么这个问题有什么解决办法吗? Thanks. 谢谢。

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 在64位系统中,所有这些值

$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" . 将被转换为相同的float(9.2233720368548E+18) ,并将其转换为字符串将得到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. 只要用于排序的索引的有效数字在它可以使用的数字的上限范围内,但这不是一种安全的排序方式。

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 基本上,我在评论中写的内容是正确的,就像我刚刚在手册中检查的那样: 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. 或将浮点数转换为字符串,但我不建议这样做,但可悲的是,这似乎是实现您要尝试执行的操作的唯一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM