This article mainly introduces the editing operations of association tables in PHP based on array functions, involving the related usage skills of PHP array comparison functions array_intersect and array_diff. Friends in need can refer to it. I hope it can help everyone.
The requirement is that when creating a school, you need to add an application, so you create a school application association table. When you edit the school and submit it, the background needs to determine whether the updated application was submitted at the beginning or if there is a new one. Application submission, old applications are deleted, simplified to an array and summarized as follows
$arr1 = array(1, 2, 4, 5, 6, 9); // 学校应用关联表中一开始的数据 $arr2 = array(3, 4, 5, 7, 8); // 前台更新的数据 /* 两个数组相同的元素,提取不变的元素 Array ( [2] => 4 [3] => 5 ) */ $arr3 = array_intersect($arr1, $arr2); print_r($arr3); /* 两个数组不同的元素,需要删除的 Array ( [0] => 1 [1] => 2 [4] => 6 [5] => 9 ) */ $arr4 = array_diff($arr1, $arr3); print_r($arr4); /* 两个数组不同的元素,需要添加的 Array ( [0] => 3 [3] => 7 [4] => 8 ) */ $arr5 = array_diff($arr2, $arr3); print_r($arr5);
Related recommendations:
Cakephp query association table Summary of methods
[PHP]Two ideas for updating intermediate related table data, php ideas_PHP tutorial
The above is the detailed content of PHP array function realizes editing of association table. For more information, please follow other related articles on the PHP Chinese website!