PHP中如何删除多维数组内图片的所有关联数据?
Hey there, let's sort out this issue for you! You've got a multi-dimensional array storing image data where each image has corresponding path, type, and thumb entries, and you want to delete an image along with all its associated data.
First, let's recap your array structure for clarity:
$_SESSION['other-image'] = [ 'img' => ['1526973657.jpg', '1526973661.jpg', '1526973665.jpg'], 'path' => ['../post-upload/1/', '../post-upload/1/', '../post-upload/1/'], 'type' => ['1', '1', '1'], 'thumb' => ['thumb_1526973661.jpg', 'thumb_1526973665.jpg', 'thumb_1526973668.jpg'] ];
Your current code only removes the entry from the img sub-array, which leaves the related path, type, and thumb entries hanging around. The fix is simple: once you find the index of the image you want to delete, you need to use that same index to remove entries from all the related sub-arrays.
Here's the corrected code:
$delkey = '1526973657.jpg'; // Check if the target image exists in the img array if (in_array($delkey, $_SESSION['other-image']['img'])) { // Get the index of the image we want to delete $imgkey = array_search($delkey, $_SESSION['other-image']['img']); // Unset the entry from ALL related sub-arrays using the same index unset( $_SESSION['other-image']['img'][$imgkey], $_SESSION['other-image']['path'][$imgkey], $_SESSION['other-image']['type'][$imgkey], $_SESSION['other-image']['thumb'][$imgkey] ); // Optional: Reindex arrays to eliminate gaps (e.g., turns [1,2] back to [0,1]) $_SESSION['other-image']['img'] = array_values($_SESSION['other-image']['img']); $_SESSION['other-image']['path'] = array_values($_SESSION['other-image']['path']); $_SESSION['other-image']['type'] = array_values($_SESSION['other-image']['type']); $_SESSION['other-image']['thumb'] = array_values($_SESSION['other-image']['thumb']); }
A quick breakdown of what's happening:
- We first confirm the image exists in the
imgsub-array usingin_array(). array_search()gives us the index (like0for your target image) where the image is stored.- We use that index to
unset()the corresponding entries in every related sub-array—this ensures no leftover data remains for the deleted image. - The
array_values()calls are optional but recommended: they reindex the arrays so you don't have missing index numbers (which can cause issues if you loop through these arrays later).
For a more dynamic solution (if you add more sub-arrays later):
If you think you might add more image-related sub-arrays down the line, you can loop through the top-level keys instead of listing each one manually:
$delkey = '1526973657.jpg'; if (in_array($delkey, $_SESSION['other-image']['img'])) { $imgkey = array_search($delkey, $_SESSION['other-image']['img']); // Loop through each top-level sub-array in the session data foreach ($_SESSION['other-image'] as $key => &$subArray) { // Only unset if the index exists in the sub-array if (isset($subArray[$imgkey])) { unset($subArray[$imgkey]); // Reindex to fix gaps $subArray = array_values($subArray); } } }
Note the &$subArray reference—this lets us modify the original session array directly instead of working with a copy.
内容的提问来源于stack exchange,提问作者user3733831

