PHP array function realizes editing of association table

小云云
Release: 2023-03-20 07:46:02
Original
1490 people have browsed it

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);
Copy after login

Related recommendations:

Cakephp query association table Summary of methods

#mysql How to obtain the value of the corresponding field in the related table when the main table is output?

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!