I have a table with four different columns. rFront,lFront,rRear and lRear. These columns store a comma deliminated list of images. I.E. image1.jpg,image2.jpg,image3.jpg etc… On my page each column can show nth amount of images. The client will have the ability to delete any selected images. Here is a the list of images from the DB that I have dumped:
$imageList Dump:
array(2) { ["lFront"]=> array(4) { [0]=> string(20)
"20180623_133503.webp" [1]=> string(20) "20180623_135209.webp" [2]=>
string(20) "20230218_091304.webp" [3]=> string(20)
"20230218_091316.webp" } ["lRear"]=> array(2) { [0]=> string(20)
"20180623_135220.webp" [1]=> string(20) "20230330_094838.webp" } }
There are 4 images in the lFront column and two images in the lRear column.
Here is the users input for deleting certain images:
$inputDelete Dump:
array(2) { ["lFrontDel"]=> array(2) { [0]=>
string(20) "20230218_091304.webp" [1]=> string(20)
"20230218_091316.webp" } ["lRearDel"]=> array(2) { [0]=> string(20)
"20180623_135220.webp" [1]=> string(20) "20230330_094838.webp" } }
I want to delete two of the images from the lFront column. The lFrontDel is the partial name from the checkbox input. The HTML attribute for it is:
value="delete[lFrontDel[]]"
value="delete[rFrontDel[]]"
value="delete[lRearDel[]]"
value="delete[rRearDel[]]"
The first arg I pass to the method is the List of images from the db. They are already exploded so they are passed as an array from the DB. The second arg passed is the $_POST[‘delete’].
I have been wracking my brain trying to use recursion and loops within loops within loops and I can’t get the result I’m needing.
The output I’m looking for is an array that I can implode for each separate column. The output array should be something like:
$diff Dump: array(2){["lFront"]=>array(2){[0]=> string(20) "20180623_133503.webp" [1]=> string(20) "20180623_135209.webp"}["rRear"]=>array(0){}}
Then I can implode each string key to create the comma deliminated list to update the db with the new list of images.
Here is a partial script that I had been working on. I have changed it so many times and I cannot seem to find a way to get it to work properly. This method almost works but only if a single image is selected from one column at a time.
protected function myArrayDiffwithDiffKeys(array $array_1,array $array_2) : array {
$diff = [];
$arrayKey = '';
// $array_1_key_1 is column name on the db for the images, $val_1 is an array
// The 4 columns are lFront,rFront,lRear and rRear
foreach($array_1 as $array_1_key_1 => $array_1_val_1){
$arrayKey = $array_1_key_1; // This is what I want returned as the key for the array: either lFront,rFront etc...
foreach($array_1_val_1 as $array_1_key_2 => $array_1_val_2){
$array1Val = $array_1_val_2;
// This will change the array 2 key to the array 1 key
foreach($array_2 as $array_2_key_1 => $array_2_val_1){
// $array_2_val_1 should always be an array
if(is_array($array_2_val_1)){
foreach($array_2_val_1 as $array_2_key_2 => $array_2_val_2){
if($array_1_val_1
$changed[$arrayKey][] = $array_2_val_2;
}
}
}
}
Remember this method is incomplete as I have been trying a bazillion different things to make it work.
2
Answers
I have gotten it to work and output the desired results. The Image List from the database was originally my Haystack since it was the larger of the two lists and the delete options from the form was the needle. I changed them around and made the delete options the haystack and the image list from the DB as the needle. By doing this, it is able to retain the proper keys on the output for DB insertion. Here is the method: Seems like I could use recursion here some how, but I don't see it.
Check this code.
I have tried the same input and here it’s the output after running the code
I think it’s the same outptu that you expected.
Don’t hesitate to ask me if you don’t understant it, and don’t forget to upvote if it helped you.