Rishikant Vishwakarma
Rishikant Vishwakarma

Reputation: 101

Php multi-dimension array issue

I am new here and also in programming , stuck in a multi-dimensional array I am posting my question array and expected output array

I have tried to make recursive function but not able to get expected answer

This is question 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
                                (
                                )

                        )

                )

        )

I want output like this , I have tried to make recursive function but it also return me the same array . so any help will highly appreciated . 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

                  )

                )

        )

I have tried this but getting same array :

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;
        }

    }
}

Upvotes: 0

Views: 62

Answers (1)

R P
R P

Reputation: 741

Here is code will help you.

    <?php

    $array1 = 'your array'

    $array2 = array(); // take a one empty array

    foreach($array1 as $key => $value){
        if(!empty($value)){
            foreach($value as $key1 => $value1){
                if(!empty($value1)){
                    $array2[$key][$key1] = array_keys_multi($value1);
                }else{
                    $array2[$key][$key1] = '';
                }
            }
        }else{
            $array2[$key] = '';
        }
    }

   // function for fetch keys in a single array.
    function array_keys_multi(array $array)
    {
        $keys = array();

        foreach ($array as $key => $value) {
            $keys[] = $key;

            if (is_array($value)) {
                $keys = array_merge($keys, array_keys_multi($value));
            }
        }

        return $keys;
    }

    echo "<pre>";
    print_r($array2);

For check and run my code http://sandbox.onlinephpfunctions.com/code/60b5b29e605b692c8f01114b9950a2d55fe3ff6a

Upvotes: 3

Related Questions