1

I have an output array like this:

Array
(
    [0] => Array
        (
            [item] => null
            [count] => 0
            [child] => Array
                (
                    [Dagadu Bocah] => Array
                        (
                            [item] => Dagadu Bocah
                            [count] => 47
                            [child] => Array
                                (
                                    [HirukPikuk] => Array
                                        (
                                            [item] => HirukPikuk
                                            [count] => 5
                                            [child] => Array
                                                (
                                                    [DGD] => Array
                                                        (
                                                            [item] => DGD
                                                            [count] => 1
                                                            [child] => 
                                                        )

                                                )

                                        )

                                    [DGD] => Array
                                        (
                                            [item] => DGD
                                            [count] => 5
                                            [child] => Array
                                                (
                                                    [Malioboroman] => Array
                                                        (
                                                            [item] => Malioboroman
                                                            [count] => 1
                                                            [child] => 
                                                        )

                                                )

                                        )

                                    [Malioboroman] => Array
                                        (
                                            [item] => Malioboroman
                                            [count] => 2
                                            [child] => 
                                        )

                                )

                        )
                )
         )
   )

in my expectations I can use the loop as I asked earlier in this question by doing repetitions in such a way as to delete certain arrays to eliminate different parts of the array which is an array that is above the array which has three items namely 'item', 'count' and 'child' and how to produce arrays like this from the array above?

    Array
(
    [0] => Array
        (
            [item] => null
            [count] => 0
            [child] => Array
                (
                    [0] => Array
                        (
                            [item] => Dagadu Bocah
                            [count] => 47
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [item] => HirukPikuk
                                            [count] => 5
                                            [child] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [item] => DGD
                                                            [count] => 1
                                                            [child] => 
                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [item] => DGD
                                            [count] => 5
                                            [child] => Array
                                                (
                                                    [Malioboroman] => Array
                                                        (
                                                            [item] => Malioboroman
                                                            [count] => 1
                                                            [child] => 
                                                        )

                                                )

                                        )

                                    [2] => Array
                                        (
                                            [item] => Malioboroman
                                            [count] => 2
                                            [child] => 
                                        )

                                )

                        )
                )
         )
   )
5
  • 1
    I don't see a question here. Commented Jan 16, 2020 at 6:40
  • If you use a recursive function with a reference parameter for the array, when you get to the level that you want to unset, it will affect the original array. Commented Jan 16, 2020 at 6:41
  • wait, I edit my question Commented Jan 16, 2020 at 7:31
  • You seem to make phrases without end. Can you split your paragraph in shorter phrases so we can understand better what you are asking? Commented Jan 16, 2020 at 8:22
  • Also, please don't post output of print_r, but post your input and expected output in PHP syntax. Commented Jan 16, 2020 at 8:23

1 Answer 1

0

It seems you want to convert the child arrays from associative arrays to indexed arrays.

Assuming your data is called $data, here is a recursive function you could use:

function convert(&$data) {
    foreach ($data as $row) {
        if(isset($row["child"])) {
            $row["child"] = array_values($row["child"]);
            convert($row["child"]);
        }
    }
}

// call it for each row
foreach($data as $row) convert($row);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.