0

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

    }
}
17
  • 2
    I am bothered by the format of the input array. It is so weird. You're sure you cannot prevent it from getting into that disordered state? Besides that, the main PHP function you would need is array_keys(). Commented Apr 24, 2019 at 10:54
  • It may be in other order and also more level deep Commented Apr 24, 2019 at 10:56
  • 1
    You do realize that the wanted output is impossible? You should always have unique array keys. I think that's a typo? Commented Apr 24, 2019 at 10:56
  • so you want to make an array, of the array keys inside a multi-dimentional array of empty arrays? Commented Apr 24, 2019 at 10:57
  • 1
    @RishikantVishwakarma can you please confirm. Your our will same logic on '190' array as array '3'. Because as per your wanted output it different in array '190' it should combine '197' and '203'. Means like [0]=> 197 [1]=> 198 [2]=> 199 [3]=> 200 [4]=> 201 [5]=> 202 [6]=> 203 [7]=> 204 [8]=> 205 [9]=> 206 [10]=> 207 Commented Apr 24, 2019 at 11:27

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.