<?php $menu = [ 'root' => [ 'items' => [ 'A' => [ 'p' => [1, 2, 9], ], 'B' => [ 'p' => [1, 2, 3, 9], ], 'C' => [ 'p' => [1, 2, 4, 9], ], 'D' => [ 'items' => [ 'D1' => [ 'p' => [1, 2, 3, 4, 9], ], 'D2' => [ 'p' => [1, 2, 3, 4], ], ], ], 'E' => [ 'items' => [ 'E1' => [ 'p' => [1, 2, 10], ], ], ], 'F' => [ 'items' => [ 'F1' => [ 'p' => [5, 6], ], 'F2' => [ 'p' => [7, 8], ], ], ], ], ], ]; function recursive_ps($elem) { $output = []; if (isset($elem['items'])) { foreach($elem['items'] as $key => $value) { if (isset($value['p'])) { $output = array_merge($output, $value['p']); } if (isset($value['items'])) { // merge the return value of function $output = array_merge($output, recursive_ps($value)); } } } return $output; } $output = array_values(array_unique(recursive_ps($menu['root']))); print_r($output);
You have javascript disabled. You will not be able to edit any code.