英文:
How do get the differences of a multi dimensional array that have different string keys?
问题
I understand that you want help with a PHP script that processes image lists stored in columns and deletes selected images based on user input. You're looking to create an output array with the remaining images for each column. Here's a partial code snippet that might help you achieve this:
protected function myArrayDiffwithDiffKeys(array $dbImages, array $deleteImages) : array {
$result = [];
// Loop through columns in the DB image list
foreach ($dbImages as $column => $images) {
// Check if there are images to delete for this column
if (isset($deleteImages[$column . 'Del'])) {
$imagesToDelete = $deleteImages[$column . 'Del'];
// Use array_diff to find the difference
$remainingImages = array_diff($images, $imagesToDelete);
// Store the remaining images in the result array
$result[$column] = array_values($remainingImages); // Re-index the array
} else {
// If no images to delete, keep the original list
$result[$column] = $images;
}
}
return $result;
}
// Usage:
$dbImages = [
"lFront" => ["20180623_133503.webp", "20180623_135209.webp", "20230218_091304.webp", "20230218_091316.webp"],
"lRear" => ["20180623_135220.webp", "20230330_094838.webp"]
];
$deleteImages = [
"lFrontDel" => ["20230218_091304.webp", "20230218_091316.webp"],
"lRearDel" => ["20180623_135220.webp", "20230330_094838.webp"]
];
$diff = myArrayDiffwithDiffKeys($dbImages, $deleteImages);
This code should give you an output array $diff
that contains the remaining images for each column after processing the user's input for deleting images. You can then implode these arrays to update the database with the new lists of images.
英文:
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.
答案1
得分: 0
我已经让它能够正常工作并输出所需的结果。数据库中的图像列表最初是我的"干草堆",因为它是两个列表中较大的一个,而表单中的删除选项则是"针"。我将它们互换了,将删除选项作为"干草堆",将来自数据库的图像列表作为"针"。通过这样做,它能够保留用于数据库插入的正确键。以下是该方法:似乎我可以在这里使用递归,但我没有看到它。
protected function myArrayDiffwithDiffKeys(array $needle, array $hay) : array {
foreach ($hay as $hay_key_1 => $hay_val_1){
foreach ($hay_val_1 as $hay_key_2 => $hay_val_2){
foreach ($needle as $needle_key_1 => $needle_val_1){
if (is_array($needle_val_1)){
foreach ($needle_val_1 as $needle_key_2 => $needle_val_2){
if ($hay_val_2 === $needle_val_2){
unset($needle[$needle_key_1][$needle_key_2]);
}
}
}
}
}
}
return $needle;
}
英文:
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.
protected function myArrayDiffwithDiffKeys(array $needle,array $hay) : array {
foreach($hay as $hay_key_1 => $hay_val_1){
foreach($hay_val_1 as $hay_key_2 => $hay_val_2){
foreach($needle as $needle_key_1 => $needle_val_1){
if(is_array($needle_val_1)){
foreach($needle_val_1 as $needle_key_2 => $needle_val_2){
if($hay_val_2 === $needle_val_2){
unset($needle[$needle_key_1][$needle_key_2]);
}
}
}
}
}
}
return $needle;
}
答案2
得分: -1
请检查这段代码。
function myArrayDiffwithDiffKeys(array $imageList, array $inputDelete): Array {
$map = [
'lFront' => 'lFrontDel',
'lRear' => 'lRearDel'
];
foreach ($imageList as $key => $images) {
if (array_key_exists($map[$key], $inputDelete)) {
for ($i = 0; $i < count($images); $i++) {
if (in_array($images[$i], $inputDelete[$map[$key]]))
unset($imageList[$key][$i]);
}
}
}
return $imageList;
}
$imageList = [
'lFront' => ["20180623_133503.webp", "20180623_135209.webp"],
'lRear' => ["20180623_135220.webp"]
];
$inputDelete = [
'lFrontDel' => ["20230218_091304.webp", "20230218_091316.webp"],
'lRearDel' => ["20180623_135220.webp"]
];
print_r(myArrayDiffwithDiffKeys($imageList, $inputDelete));
英文:
Check this code.
<?php
function myArrayDiffwithDiffKeys(array $imageList,array $inputDelete): Array {
$map = [
'lFront' => 'lFrontDel',
'lRear' => 'lRearDel'
];
foreach($imageList as $key => $images) {
if(array_key_exists($map[$key], $inputDelete)){
for($i = 0; $i < count($images); $i++) {
if(in_array($images[$i], $inputDelete[$map[$key]]))
unset($imageList[$key][$i]);
}
}
}
return $imageList;
}
$imageList = [
'lFront' => ["20180623_133503.webp", "20180623_135209.webp", "20230218_091304.webp", "20230218_091316.webp"],
'lRear' => ["20180623_135220.webp", "20230330_094838.webp"]
];
$inputDelete = [
'lFrontDel' => ["20230218_091304.webp", "20230218_091316.webp"],
'lRearDel' => ["20180623_135220.webp", "20230330_094838.webp"]
];
print_r(myArrayDiffwithDiffKeys($imageList, $inputDelete));
?>
I have tried the same input and here it's the output after running the code
Array
(
[lFront] => Array
(
[0] => 20180623_133503.webp
[1] => 20180623_135209.webp
)
[lRear] => Array
(
)
)
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论