0

I would like to try and make my array display like:

Parent | sub | sub | ...

Below is the array Essentially, I'm trying to create a csv (I can create the csv, but I cant figure out the best way to sift through each element of the array to create a string)

[0] => Array
            (
                [id] => 79
                [parent_id] => 0
                [name] => Accessories
                [is_visible] => 1
                [url] => /bedroom-wear/accessories/
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 152
                                [parent_id] => 79
                                [name] => Chokers
                                [is_visible] => 1
                                [url] => /bedroom-wear/accessories/chokers/
                                [children] => Array
                                    (
                                    )

                            )

                        [1] => Array
                            (
                                [id] => 80
                                [parent_id] => 79
                                [name] => Garters
                                [is_visible] => 1
                                [url] => /bedroom-wear/accessories/garters/
                                [children] => Array
                                    (
                                    )

                            )

                        [2] => Array
                            (
                                [id] => 113
                                [parent_id] => 79
                                [name] => Gloves
                                [is_visible] => 1
                                [url] => /bedroom-wear/accessories/gloves/
                                [children] => Array
                                    (
                                    )

                            )

                        [3] => Array
                            (
                                [id] => 450
                                [parent_id] => 79
                                [name] => Masks
                                [is_visible] => 1
                                [url] => /accessories/masks/
                                [children] => Array
                                    (
                                    )

                            )

                        [4] => Array
                            (
                                [id] => 135
                                [parent_id] => 79
                                [name] => Nipple Covers
                                [is_visible] => 1
                                [url] => /bedroom-wear/accessories/nipple-covers/
                                [children] => Array
                                    (
                                    )

                            )

                        [5] => Array
                            (
                                [id] => 586
                                [parent_id] => 79
                                [name] => Pride
                                [is_visible] => 1
                                [url] => /accessories/pride/
                                [children] => Array
                                    (
                                        [0] => Array
                                            (
                                                [id] => 587
                                                [parent_id] => 586
                                                [name] => Confetti
                                                [is_visible] => 1
                                                [url] => /accessories/pride/confetti/
                                                [children] => Array
                                                    (
                                                    )

                                            )

                                    )

                            )

                    )

            )

Desired output:

Accessories | Chokers
Accessories | Garters
Accessories | Gloves
Accessories | ...
Accessories | ...
Accessories | Pride | Confetti

I can't even begin to think where to start, if anyone could point me in the right direction that would be great <3

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • There is no magic/built-in method that does this for you, you have to create a loop and go through each element and extract the data you want. – catcon Aug 13 '21 at 01:17
  • 1
    Does this answer your question? [How can I access an array/object?](https://stackoverflow.com/questions/30680938/how-can-i-access-an-array-object) – catcon Aug 13 '21 at 01:17
  • I was hoping there was like an easier way of looping through the array, I know how to work with them. Eventually I settled with a multitude of foreach loops within each other - done the job I wanted... It's annoying because I just wanted to to paste the output into a spreadsheet anyway :D Thanks anyway though – user2405660 Aug 13 '21 at 01:36
  • 1
    https://stackoverflow.com/q/2749398/2943403 , https://stackoverflow.com/q/32578612/2943403 , https://stackoverflow.com/q/35893582/2943403 – mickmackusa Aug 13 '21 at 04:20

1 Answers1

0

Okay I've put together the most rudimentary foreach loop thee world has seen, and yes I hate myself for it, but just in case anyone wants a dirty way to do this:

foreach($response['data'] as $r){
  echo $r['name'] . "<br>";
  foreach($r['children'] as $child){
    echo $r['name'] . " | " . $child['name'] . "<br>";
    foreach($child['children'] as $a){
      echo $r['name'] . " | " . $child['name'] . " | " . $a['name'] . "<br>";
      foreach($a['children'] as $b){
        echo $r['name'] . " | " . $child['name'] . " | " . $a['name'] . " | " . $b['name'] . "<br>";
        foreach($b['children'] as $c){
          echo $r['name'] . " | " . $child['name'] . " | " . $a['name'] . " | " . $b['name'] . " | " . $c['name'] . "<br>";
          foreach($c['children'] as $d){
            echo $r['name'] . " | " . $child['name'] . " | " . $a['name'] . " | " . $b['name'] . " | " . $c['name'] . " | " . $d['name'] . "<br>";
          }
        }
      }
    }
  }
}

Bare in mind you have to create a foreach for the max number of sub arrays

Output:

Accessories
Accessories | Chokers
Accessories | Garters
Accessories | Gloves
Accessories | Masks
Accessories | Nipple Covers
Accessories | Pride
Accessories | Pride | Confetti