I have a foreach loop set up to go through my array, check for a certain link, and if it finds it removes that link from the array.
My code:
foreach($images as $image) { if($image == 'http://i27.tinypic.com/29yk345.gif' || $image == 'http://img3.abload.de/img/10nx2340fhco.gif' || $image == 'http://i42.tinypic.com/9pp2456x.gif') { unset($images[$image]); } }
But it doesn't remove the array entires. It's probably something to do with $images[$image]
, as that's not the key of the array entry, only the content? Is there a way to do this without incorporating a counter?
Thanks.
EDIT: Thanks guys, but now I have another problem where the array entries don't actually get deleted.
My new code:
foreach($images[1] as $key => $image) { if($image == 'http://i27.tinypic.com/29yk345.gif') $image == 'http://img3.abload.de/img/10nx2340fhco.gif' || $image == 'http://i42.tinypic.com/9pp2456x.gif') { unset($images[$key]); } }
$images is actuallty a two-dimensional array now hence why I need $images[1]. I have checked and it successfully goes around the array elements, and some elements do actually have some of those URLs in that I wish to delete, but they're not getting deleted. This is my $images
array:
Array ( [0] => Array ( [0] => useless [1] => useless [2] => useless [3] => useless [4] => useless ) [1] => Array ( [0] => http://i27.tinypic.com/29yk345.gif [1] => http://img3.abload.de/img/10nx2340fhco.gif [2] => http://img3.abload.de/img/10nx2340fhco.gif [3] => http://i42.tinypic.com/9pp2456x.gif ) )
Thanks!
Remove duplicate values from a while loop. Simply you can push all elements to one array and then remove the duplicates and then get values as you need. Use your foreach to assign the correct values to an array.
To remove duplicates from an array: First, convert an array of duplicates to a Set . The new Set will implicitly remove duplicate elements. Then, convert the set back to an array.
foreach($images as $key => $image) { if(in_array($image, array( 'http://i27.tinypic.com/29ykt1f.gif', 'http://img3.abload.de/img/10nxjl0fhco.gif', 'http://i42.tinypic.com/9pp2tx.gif', )) { unset($images[$key]); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With