简体   繁体   English

是否可以使用自定义编写的 php 代码查看使用 usort 对数组进行排序的阶段?

[英]Is it possible to see the stages of sorting an array with usort with a custom written php code?

Lets say any array but it can be applying only to this piece of code.可以说任何数组,但它只能应用于这段代码。 Based on the info from here I know that if there is 6-15 elements in an array the sorting algorithm used would be Insertion Sort and for other number of elements it would be the Quicksort:根据这里的信息,我知道如果数组中有 6-15 个元素,则使用的排序算法将是插入排序,而对于其他数量的元素,它将是快速排序:

https://github.com/php/php-src/blob/eac0bf11e4e97916e9688b18e6d62572e12e129f/Zend/zend_sort.c#L176 https://github.com/php/php-src/blob/eac0bf11e4e97916e9688b18e6d62572e12e129f/Zend/zend_sort.c#L176

<?php
function testing($a,$b){
    if ($a < $b ){
        return -1;
    }
    elseif ($a > $b){
        return 1;
    }
    //else {
        //return 0;
    //}
}

$array = array(1,3,2,4,5);
usort($array, "testing");
var_dump($array);
?>

People have been saying that I am overthinking this and that this is not needed, but seeing every stage of sorting the array would be the best representation of how the algorithm works (it is not that easy to figure this out from the $a-$b pairs that I can output with echo or something like that).人们一直在说我想多了,这不是必需的,但是看到对数组进行排序的每个阶段将是算法工作方式的最佳表示(从 $a-$ 中找出这一点并不容易b 对,我可以用 echo 或类似的东西输出)。 var_dump will not show me the stages of sorting an array, it will be always the same. var_dump 不会向我展示排序数组的阶段,它总是相同的。

Again, this is the only goal of this question - achieve the access to viewing the stages of sorting (any / an) array.同样,这是这个问题的唯一目标 - 实现查看排序(任何/一个)数组的阶段的访问权限。

Somebody has suggested something like this but I have not been able to figure this out, it may not be a doable thing:有人提出了这样的建议,但我无法弄清楚,这可能不是一件可行的事情:

"You could try with an anonymous function referencing the array (usort($arr, function ($a, $b) use ($arr) { ... })) and output the array every step of the way too… I'm not sure whether the result would be reflected immediately or not though." “您可以尝试使用引用数组的匿名函数 (usort($arr, function ($a, $b) use ($arr) { ... })) 并在每一步输出数组......我'我不确定结果是否会立即反映出来。”

Thanks.谢谢。

No, it isn't possible.不,这是不可能的。

A copy of the input array is created in usort, so you won't be able to observe what it's doing via PHP code.输入数组的副本是在 usort 中创建的,因此您将无法通过 PHP 代码观察它在做什么。

See in the PHP source code for usort :PHP 源代码中查看 usort

/* Copy array, so the in-place modifications will not be visible to the callback function */
arr = zend_array_dup(arr);

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

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