php按键合并多维数组排序

时间:2021-08-13 10:45:18

I'm trying to sort a multidimensional array down to a sorted, one-dimensional array where values on the same level are merged together ordered by key alternating between it's parents.

我正在尝试将多维数组排序到一个排序的一维数组,其中同一级别的值合并在一起,由它的父级之间的键交替排序。

So start with this array:

所以从这个数组开始:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 60
                    [1] => 68
                    [2] => 71
                    [3] => 72
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => 61
                    [1] => 62
                    [2] => 64
                )

            [1] => Array
                (
                    [0] => 69
                    [1] => 70
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [0] => 63
                )

            [1] => Array
                (
                    [0] => 65
                    [1] => 66
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [0] => 66
                )

        )

)

and end up with this:

并以此结束:

Array
(
    [0] => 60
    [1] => 68
    [2] => 71
    [3] => 72
    [4] => 61
    [5] => 69
    [6] => 62
    [7] => 70
    [8] => 64
    [9] => 63
    [10] => 65
    [11] => 66
    [12] => 67
)

I tried something from this question like this:

我从这个问题尝试了这样的东西:

function merge_common_keys(){
    $arr = func_get_args();
    $num = func_num_args();

    $keys = array();
    $i = 0;
    for($i=0;$i<$num;++$i){
        $keys = array_merge($keys, array_keys($arr[$i]));
    }

    $keys = array_unique($keys);

    $merged = array();

    foreach($keys as $key){
        for($i=0;$i<$num;++$i){
            if(isset($arr[$i][$key])){
                $merged[] = $arr[$i][$key];
            }
        }
    }

    return $merged;
}

but it requires passing in multiple arrays and I can't figure out how to feed it just one big array and recursive walk down it.

但它需要传递多个数组,我无法弄清楚如何只给一个大数组提供它并递归走下去。

2 个解决方案

#1


0  

This is what I came up with:

这就是我提出的:

function array_builder($array) {
    $output = array();
    foreach($array as $level1) {
        if(count($level1) > 1) {
            $counts = array();
            foreach($level1 as $level2) {
                $counts[] = count($level2);
            }
            $largest = max($counts);
            $level2_count = count($level1);
            for($x=0;$x<$largest;$x++) {
                for($y=0;$y<$level2_count;$y++) {
                    if(isset($level1[$y][$x])) {
                        $output[] = $level1[$y][$x];
                    }
                }
            }
        } else {
            $output = array_merge($output,$level1[0]);
        }
    }
    return $output;
}

A print_r() will give you this:

print_r()会给你这个:

Array ( 
    [0] => 60 
    [1] => 68 
    [2] => 71
    [3] => 72
    [4] => 61
    [5] => 69
    [6] => 62
    [7] => 70
    [8] => 64
    [9] => 63
    [10] => 65
    [11] => 66
    [12] => 66 
)

#2


0  

An attempt with php 5.4: (may have been shorter on php 5.5 with array_column).

尝试使用php 5.4 :(可能在php 5.5上使用array_column更短)。

function flattenAndVentilate(array $array)
{
    $result = array();
    foreach ($array as $array_l1) {
        $copy = $array_l1;
        // get level2 count()s in $copy array
        array_walk($copy, function(&$value,$key){$value=count($value);});
        $maxsize = max($copy);
        for ($i=0;$i<$maxsize;$i++) {
            foreach ($array_l1 as $array_l2) {
                if (isset($array_l2[$i])) {
                    $result[] = $array_l2[$i];
                }
            }
        }
    }

    return $result;
}

print_r(flattenAndVentilate($array));

outputs :

Array
(
    [0] => 60
    [1] => 68
    [2] => 71
    [3] => 72
    [4] => 61
    [5] => 69
    [6] => 62
    [7] => 70
    [8] => 64
    [9] => 63
    [10] => 65
    [11] => 66
    [12] => 66
)

#1


0  

This is what I came up with:

这就是我提出的:

function array_builder($array) {
    $output = array();
    foreach($array as $level1) {
        if(count($level1) > 1) {
            $counts = array();
            foreach($level1 as $level2) {
                $counts[] = count($level2);
            }
            $largest = max($counts);
            $level2_count = count($level1);
            for($x=0;$x<$largest;$x++) {
                for($y=0;$y<$level2_count;$y++) {
                    if(isset($level1[$y][$x])) {
                        $output[] = $level1[$y][$x];
                    }
                }
            }
        } else {
            $output = array_merge($output,$level1[0]);
        }
    }
    return $output;
}

A print_r() will give you this:

print_r()会给你这个:

Array ( 
    [0] => 60 
    [1] => 68 
    [2] => 71
    [3] => 72
    [4] => 61
    [5] => 69
    [6] => 62
    [7] => 70
    [8] => 64
    [9] => 63
    [10] => 65
    [11] => 66
    [12] => 66 
)

#2


0  

An attempt with php 5.4: (may have been shorter on php 5.5 with array_column).

尝试使用php 5.4 :(可能在php 5.5上使用array_column更短)。

function flattenAndVentilate(array $array)
{
    $result = array();
    foreach ($array as $array_l1) {
        $copy = $array_l1;
        // get level2 count()s in $copy array
        array_walk($copy, function(&$value,$key){$value=count($value);});
        $maxsize = max($copy);
        for ($i=0;$i<$maxsize;$i++) {
            foreach ($array_l1 as $array_l2) {
                if (isset($array_l2[$i])) {
                    $result[] = $array_l2[$i];
                }
            }
        }
    }

    return $result;
}

print_r(flattenAndVentilate($array));

outputs :

Array
(
    [0] => 60
    [1] => 68
    [2] => 71
    [3] => 72
    [4] => 61
    [5] => 69
    [6] => 62
    [7] => 70
    [8] => 64
    [9] => 63
    [10] => 65
    [11] => 66
    [12] => 66
)