Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear
CREATE TABLE categories ( id INT PRIMARY KEY, name VARCHAR(255) NOT NULL, parent INT ); INSERT INTO categories (id, name, parent) VALUES (1, 'shoes', 0), (2, 'women_shoes', 1), (3, 'man_shoes', 1), (4, 'sweets', 0), (5, 'man_sweets', 4), (6, 'women_sweets', 4), (7, 'women_red_sweets', 6), (8, 'women_green_sweets', 6), (9, 'men_blue_sweets', 5);

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php function categories_list($mysqli, $id = 0) { $list = []; $result = $mysqli->execute_query("SELECT * FROM categories WHERE parent = ?", [$id]); foreach ($result as $value) { $list[] = $value['id']; array_push($list, ...categories_list($mysqli, $value['id'])); } return array_unique($list); } var_export(categories_list($mysqli));
Copy Clear