PHP多维数组问题

时间:2019-04-24 10:51:16

标签: php multidimensional-array

我在这里还是编程方面的新手,都陷在多维数组中,我正在发布问题数组和预期的输出数组

我试图做递归函数,但无法获得预期的答案

这是问题数组

Array
(
    [3] => Array
        (
            [4] => Array
                (
                    [5] => Array
                        (
                            [6] => Array
                                (
                                )

                            [7] => Array
                                (
                                )

                            [8] => Array
                                (
                                )

                            [9] => Array
                                (
                                )

                        )

                    [10] => Array
                        (
                            [11] => Array
                                (
                                )

                            [12] => Array
                                (
                                )

                            [13] => Array
                                (
                                )

                            [14] => Array
                                (
                                )

                        )

                )

            [20] => Array
                (

                    [26] => Array
                        (
                            [27] => Array
                                (
                                )

                            [28] => Array
                                (
                                )

                            [29] => Array
                                (
                                )

                            [30] => Array
                                (
                                )

                        )

                )

            [48] => Array
                (


                    [53] => Array
                        (
                            [54] => Array
                                (
                                )

                            [55] => Array
                                (
                                )

                            [56] => Array
                                (
                                )

                            [57] => Array
                                (
                                )

                        )

                )



        )


    [190] => Array
        (
            [191] => Array
                (

                    [197] => Array
                        (
                            [198] => Array
                                (
                                )

                            [199] => Array
                                (
                                )

                            [200] => Array
                                (
                                )

                            [201] => Array
                                (
                                )

                            [202] => Array
                                (
                                )

                        )

                    [203] => Array
                        (
                            [204] => Array
                                (
                                )

                            [205] => Array
                                (
                                )

                            [206] => Array
                                (
                                )

                            [207] => Array
                                (
                                )

                        )

                )

        )

我想要这样的输出,我试图做递归函数,但它也返回相同的数组。因此,任何帮助将不胜感激。 Tq

 Array
(
    [3] => Array
        (
            [4] => Array
                (
                [0]=> 5
                [1]=> 6
                [3]=> 7
                [4]=> 8
                [5]=> 9
                [6]=> 10
                [7]=> 11
                [8]=> 12
                [9]=> 13
                [10]=> 14

                )

            [20] => Array
                (

                [0]=> 26
                [1]=> 27
                [2]=> 28
                [3]=> 29
                [4]=> 30

                )

            [48] => Array
                (
                [0]=> 53
                [1]=> 54
                [2]=> 55
                [3]=> 56
                [4]=> 57
                )

        )


    [190] => Array
        (
            [191] => Array
                (
                [0]=> 197
                [1]=> 198
                [2]=> 199
                [3]=> 200
                [4]=> 201
                [5]=> 202
                [6]=> 203
                [7]=> 204
                [8]=> 205
                [9]=> 206
                [10]=> 207

                  )

                )

        )

我已经尝试过但是得到相同的数组:

public function prepareFunction($array)
{

    foreach ($array as $key => $value) {
        if (is_array($value) && !empty($value)) {
            $this->getAllNestedChild($value, $key);
        } else {
            $this->global_array[$key][] = $value;
        }
    }

    return $this->global_array;
}


public function getAllNestedChild($array, $direct_connected)
{
    foreach ($array as $key => $value) {
        if (is_array($value) && !empty($value)) {
            $this->global_array[$direct_connected][$key] = $value;
            $this->getAllNestedChild($value, $direct_connected);
        } else {
            $this->global_array[$direct_connected][$key] = $value;
        }

    }
}

1 个答案:

答案 0 :(得分:3)

以下代码将为您提供帮助。

            string input1 = "1,1,0,0,0,0,0,0,0,0,0,0";
            string input2 = "\u003F,\u003F,1,\u003F,\u003F,\u003F,\u003F,\u003F,\u003F,\u003F,\u003F,\u003F";

            int results = input1.Select((x, i) => new { chr = x, index = i }).Where(x => (x.chr != input2[x.index]) && (x.chr != '\u003F') && (input2[x.index] != '\u003F')).Min(x => x.index);

查看并运行我的代码http://sandbox.onlinephpfunctions.com/code/60b5b29e605b692c8f01114b9950a2d55fe3ff6a