SELECT * FROM `parents1`;
+----+-------+
| id | name |
+----+-------+
| 1 | asdf |
| 2 | asdf2 |
| 3 | asdf3 |
| 4 | asdf4 |
+----+-------+
SELECT * FROM `children1`;
+----+------+-----------+
| id | name | parent_id |
+----+------+-----------+
| 1 | sdf | 0 |
| 2 | sdf2 | 0 |
| 3 | sdf2 | 1 |
| 4 | sdf3 | 3 |
| 5 | sdf4 | 1 |
| 6 | sdf5 | 1 |
+----+------+-----------+
DELIMITER //
CREATE TRIGGER `parents_children` AFTER DELETE ON `parents1` FOR EACH ROW
BEGIN
DELETE FROM `children1` WHERE `parent_id`=old.`id`;
END//
DELIMITER ;
DELETE FROM `parents1` WHERE `id`=1;
SELECT * FROM `parents1`;
+----+-------+
| id | name |
+----+-------+
| 2 | asdf2 |
| 3 | asdf3 |
| 4 | asdf4 |
+----+-------+
SELECT * FROM `children1`;
+----+------+-----------+
| id | name | parent_id |
+----+------+-----------+
| 1 | sdf | 0 |
| 2 | sdf2 | 0 |
| 4 | sdf3 | 3 |
+----+------+-----------+
<?php
function recursion_func(array &$parents1, array &$children1, int $begin=0) : array {
$parents2 = $parents1;
// echo $parents2[$begin] . ' ' . $children1[key($children1)] . ' ' . key($children1) . ' ' . (count($children1)-1) . '<br />';
if($parents2[$begin] == $children1[key($children1)] && key($children1) == count($children1)-1) {
$parents1[$begin] = 0;
$children1[key($children1)] = 0;
recursion_func($parents2, $children1, $begin);
} else if($parents2[$begin] == $children1[key($children1)] && key($children1) != (count($children1)-1)) {
$parents1[$begin] = 0;
$children1[key($children1)] = 0;
next($children1);
recursion_func($parents2, $children1, $begin);
} else if(key($children1)!=count($children1)-1) { // $parents2[$begin] != $children1[key($children1)] && key($children1) != (count($children1)-1)
next($children1);
recursion_func($parents1, $children1, $begin);
} else {
reset($children1);
}
return [$parents1, $children1];
}
$parents11 = [8,1,2,3,4,2,5]; // here must be unique values or you must use array_unique function
$parents11 = array_values(array_unique($parents11));
$children11 = [8,0,2,3,3,3,2,0,1,5];
var_dump($parents11);
for($begin=0;$begin<sizeof($parents11);$begin++) {
$x = recursion_func($parents11, $children11, $begin);
}
print_r($x);
// https://www.quora.com/How-do-I-copy-an-array-in-PHP
// https://stackoverflow.com/questions/9031076/php-remove-reference-from-a-variable
?>
link link
Możliwe, że dałoby się to jakimś map'em napisać bez pisania własnej funkcji rekurencyjnej.
Potestuj, żeby nie było czasem rekurencji nieskończonej (infinity).