Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于另一个数组对二维数组进行排序_Php_Arrays_Sorting_Multidimensional Array - Fatal编程技术网

Php 基于另一个数组对二维数组进行排序

Php 基于另一个数组对二维数组进行排序,php,arrays,sorting,multidimensional-array,Php,Arrays,Sorting,Multidimensional Array,我有一个一维阵列和一个二维阵列 arrayA[0] = 'D' arrayA[1] = 'U' arrayA[3] = 'R' arrayA[4] = 'B' arrayA[5] = 'S' arrayA[6] = 'H' arrayB[0][0] = 'D' arrayB[0][1] = 2 arrayB[1][0] = 'B' arrayB[1][1] = 1 arrayB[2][0] = 'R' arrayB[2][1] = 1 arrayB[3][0] = 'U'

我有一个一维阵列和一个二维阵列

arrayA[0] = 'D'
arrayA[1] = 'U'
arrayA[3] = 'R'
arrayA[4] = 'B'
arrayA[5] = 'S'
arrayA[6] = 'H'

arrayB[0][0] = 'D'   arrayB[0][1] = 2   
arrayB[1][0] = 'B'   arrayB[1][1] = 1
arrayB[2][0] = 'R'   arrayB[2][1] = 1
arrayB[3][0] = 'U'   arrayB[3][1] = 1
arrayB[4][0] = 'H'   arrayB[4][1] = 0
arrayB[5][0] = 'S'   arrayB[5][1] = 0
arrayB[x][y]是基于y排序的

我必须使用字母创建一个新数组,首先从arrayB[x][y]中优先考虑y。 但y中也有一些相同的值,比如1的三倍和0的两倍。 在这种情况下,将根据arrayA对类似值进行排序

新阵列的排序如下:

D, U, R, B, S, H
我怎样才能有效地做到这一点


提前谢谢

我试过这个,效果很好。a1是一维数组,a2是二维数组

编辑:现在可以了

function sortArr($a1, $a2) {
    $t1 = array();

    foreach ($a1 as $v) {
        $t2 = array();
        foreach ($a2 as $i=>$k) {
            if ($k[0] == $v) {
                $t2[0] = $k[0];
                $t2[1] = $k[1];
                $t1[$i] = $t2;
                break;
            }
        }
    }

    return $t1;
}

在下面的代码中,
$string
对应于
arrayB[x][0]
$weight
对应于
arrayB[x][1]
$order
对应于
arrayA

<?php

$string = str_split('DBRUHS');
$weight = str_split('211100');
$result = '';

function my_sort($a, $b)
{
    $order = str_split('DURBSH');
    $a = array_search($a, $order);
    $b = array_search($b, $order);
    if ($a === $b) return 0;
    if ($a === FALSE) return -1;
    if ($b === FALSE) return 1;
    if ($a == $b) return 0;
    return ($a < $b) ? -1 : 1;
}

arsort($weight);
$current_priority = -1;
$substring = array();
$max = count($weight);
$count = 1;
foreach ($weight as $position => $priority)
{
    if ($count++ == $max)
    {
        $substring[] .= $string[$position];
        $priority = $current_priority + 1;
    }
    if ($current_priority != $priority)
    {
        if (!empty($substring))
        {
            usort($substring, 'my_sort');
            $result .= implode('', $substring);
        }
        $current_priority = $priority;
        $substring = array($string[$position]);
    }
    else
    {
        $substring[] = $string[$position];
    }
}

print_r(str_split($result));

?>


您不使用关联数组的具体原因是什么<例如,code>arrayB['D']=2你是不是故意在arrayA中遗漏了偏移量2?我忽略了一些东西。给我一点时间。哦,谢谢,现在可以了。我非常需要它,但我的大脑由于承受压力而无法工作:)对不起,如果你改变阵列,这个解决方案就不起作用了。只需做一个简单的测试,更改arrayA中的顺序或元素,您就会看到。是的,它并不适合所有条件。您可以使用arrayA($order)和arrayB($string和$weight)的任何序列测试上述代码。它确实有效。您只需要将这些变量替换为数组的内容。(我使用
stru-split
只是为了方便起见,所以我不需要键入整个数组。)