In an edit form I have the possibility to add jobs and to match the reasons for the job (not necessarily). The first thing I do is to retrieve the current state, before the modification, so as to have an array like this from my query:
Array (
[1] => Array (
[0] => 3
[1] => 4
)
[3] => Array (
[0] => 11
)
)
Where the numeric value of the key represents the id of the job and its value in array the id of any reasons. So in the example, work with id = 1 has the reasons with id 3 and 4, work with id = 3 has the reason with id 11 and so on
From the form I get an array with the ids of the jobs posted like this
Array (
[0] => 1
[1] => 3
)
And for each job an array that contains its reasons like this
ids_reasons_1 = Array (
[0] => 3
[1] => 4
)
ids_reasons_3 = Array ( [0] => 11 )
Now I should detect any changes: If I have added a job, if I have deleted a job and if I have added or removed some reasons from the pre-existing jobs
The first approach I tried is to use array_key_exists, to determine if a job was pre-existing or if it is new
if(array_key_exists(job_id, array_value_from_database):
If it returns true, it is a job that already exists, otherwise it is a new job, but I don't know how to determine if an existing job has been deleted
Obviously, within the hypothetical foreach I should also check if the reasons for each posted job have changed compared to those previously saved in the database
Could you show me the way to get any changes to what was previously saved both as regards the work and for its reasons
The need is to track who does what during these types of changes
array_diff.array_keys. Then you can compare the difference between the keys of the database array and values of the new job array, just like you would any other two arrays.