简体   繁体   English

php array_multisort使用数组名称作为参数

[英]php array_multisort use array name as argument

Sorry if this is duplicate, I have tried to look at previous posts, but maybe my limited understanding of php gets in the way. 抱歉,如果这是重复的话,我试图看一下以前的文章,但也许​​我对php的有限理解会妨碍您。

This works to sort arrays 1 and 3 by array 2: 这可以按数组2对数组1和3进行排序:

<?php

$ar[1] = array("game","scissors","rock","paper");
$ar[2] = array("D", "B", "A", "C");
$ar[3] =array("four","two","one","three");

array_multisort($ar[2],$ar[1],$ar[3]);

for ($j=0;$j<=3;$j++) {

    for ($i=1;$i<=3;$i++) {
  echo $ar[$i][$j]." ";
}
echo "\n";
}

?>

output: 输出:

rock A one 摇滚一
scissors B two 剪刀B两
paper C three 纸三
game D four 游戏四

Fine. 精细。 But I want the list of arrays used as argument for array_multisort() to be specified by user input, so it can't be literal. 但是我希望用作数组array_multisort()参数的数组列表由用户输入指定,因此它不能是字面的。 Instead, I would like the program to decide the order of arrays in the list based on the user input and then name the resulting array of arrays $supar (superarray). 相反,我希望程序根据用户输入来确定列表中数组的顺序,然后命名数组$ supar(superarray)的结果数组。 But when using $supar as argument for array_multisort() , no sorting is carried out. 但是,当使用$ supar作为array_multisort()参数时,不会进行排序。

<?php

$ar[1] = array("game","scissors","rock","paper");
$ar[2] = array("D", "B", "A", "C");
$ar[3] =array("four","two","one","three");


$supar = array($ar[2],$ar[1],$ar[3]);
array_multisort($supar);

for ($j=0;$j<=3;$j++) {

    for ($i=1;$i<=3;$i++) {
  echo $ar[$i][$j]." ";
}
echo "\n";
}

?>

Output: 输出:

game D four 游戏四
scissors B two 剪刀B两
rock A one 摇滚一
paper C three 纸三

How should I do to make array_multisort() sort the arrays in $supar by a specified $ar array? 如何使array_multisort()按指定的$ ar数组对$ supar中的数组排序?

Thank you for your patience. 感谢您的耐心等待。

You can't do that with just the PHP function. 您不能仅使用PHP函数来做到这一点。 array_multisort() takes the different arrays separately as arguments and you can't just pass a different thing with the intended arguments inside. array_multisort()将不同的数组作为参数,并且您不能只传递带有预期参数的其他东西。

What you could do is writing your own function to do that for you. 您可以做的是编写自己的函数来为您执行此操作。 Something like: 就像是:

function my_multisort($supar) {
    array_multisort($supar[2], $supar[1], $supar[3]);
}

$supar = array(
    array("game","scissors","rock","paper"),
    array("D", "B", "A", "C"),
    array("four","two","one","three")
);

my_multisort($supar);

EDIT 编辑

As @axiac correctly pointed out, this is basically a weird specific way of reinventing call_user_func_array() . 正如@axiac正确指出的那样,这基本上是重塑call_user_func_array()的怪异方法。 You can simply build your array of arguments $supar as you need and then do 您可以根据需要简单地构建参数数组$supar ,然后执行

call_user_func_array('array_multisort', $supar);

You can call array_multisort() (or any other function) without specifying its arguments explicitly if you have the arguments in an array by using call_user_func_array() . 如果您使用call_user_func_array()在数组中包含参数,则可以在不显式指定其参数的情况下调用array_multisort() (或任何其他函数call_user_func_array()

array_multisort() gets its arguments by reference (in order to be able to modify them). array_multisort()通过引用获取其参数(以便能够对其进行修改)。 You have to consider this when you build the array you use as argument to call_user_func_array() and put references in this array: 在构建用作call_user_func_array()参数的数组并将引用放在此数组中时,必须考虑这一点:

// Input arrays
$ar[1] = array("game", "scissors", "rock", "paper");
$ar[2] = array("D", "B", "A", "C");
$ar[3] = array("four", "two", "one", "three");

// Build the list of arguments for array_multisort()
// Use references to the arrays you want it to sort
$supar = array(&$ar[2], &$ar[1], &$ar[3]);

// Call array_multisort() indirectly
// This is the same as array_multisort($ar[2], $ar[1], $ar[3]);
call_user_func_array('array_multisort', $supar);

Check it in action: https://3v4l.org/ptiog 对其进行检查: https : //3v4l.org/ptiog

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

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