recursive array - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of recursive array.php
- <?php
- //given an array like this:
- $categories = [
- 'Mobile' => [
- 'Phones' => [
- 'Android phones',
- 'Apple iPhone'
- ],
- 'Tablets' => [
- 'Android tablets',
- 'Apple iPad'
- ],
- ],
- 'Photo & Video' => [
- 'Action Cameras' => [
- 'GoPro',
- 'Sony'
- ],
- 'Storage & memory' => [
- 'SD Cards',
- 'USBs'
- ]
- ],
- 'Gaming' => [
- 'Playstation' => [
- 'Playstation 4' => [
- 'Playstation 4 Games',
- 'Playstation 4 Consoles'
- ],
- 'Playstation VR' => [
- 'Playstation VR Games',
- 'Playstation VR Accessories'
- ]
- ],
- 'Xbox One' => [
- 'Xbox One Games'
- ]
- ],
- ];
- /*
- Output a tree/sitemap like this:
- Mobile
- Phones
- Android phones
- Apple iPhone
- Tablets
- Android tablets
- Apple iPad
- Photo & Video
- Action Cameras
- GoPro
- Sony
- Storage & memory
- SD Cards
- USBs
- Gaming
- Playstation
- Playstation 4
- Playstation 4 Games
- Playstation 4 Consoles
- Playstation VR
- Playstation VR Games
- Playstation VR Accessories
- Xbox One
- Xbox One Games
- */
- function arrayHelper($array){
- echo "<ul>";
- foreach ($array as $attribute => $value){
- if (is_array($value)){
- echo "<li>{$attribute}";
- arrayHelper($value);
- echo "</li>";
- }else{
- echo "<li>{$value}</li>";
- }
- }
- echo "</ul>";
- }
- arrayHelper($categories);
File Description
- recursive array
- PHP Code
- 12 Apr-2021
- 1.68 Kb
You can Share it:
Latest PHP Pastes