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]);
}
}
Answer from hsz on Stack Overflowforeach($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]);
}
}
Try that:
foreach ($images[1] as $key => &$image) {
if (yourConditionGoesHere) {
unset($images[1][$key])
}
}
unset($image); // detach reference after loop
Normally, foreach operates on a copy of your array so any changes you make, are made to that copy and don't affect the actual array.
So you need to unset the values via $images[$key];
The reference on &$image prevents the loop from creating a copy of the array which would waste memory.
If you also get the key, you can delete that item like this:
foreach ($display_related_tags as
tag_name) {
if($tag_name == $found_tag['name']) {
unset($display_related_tags[$key]);
}
}
A better solution is to use the array_filter function:
$display_related_tags =
array_filter($display_related_tags, function(
found_tag){
return
found_tag['name'];
});
As the php documentation reads:
As foreach relies on the internal array pointer in PHP 5, changing it within the loop may lead to unexpected behavior.
In PHP 7, foreach does not use the internal array pointer.
You're unsetting the reference (breaking the reference). You'd need to unset based on a key:
foreach ($this->result['list'] as $key => &$row) {
if ($this_row_is_boring) {
unset($this->result['list'][$key]);
}
}
foreach ($this->result['list'] as $key => &$row) {
if ($this_row_is_boring) {
unset($this->result['list'][$key]);
}
}
unset($row);
Remember: if you are using a foreach with a reference, you should use unset to dereference so that foreach doesn't copy the next one on top of it. More info
You need to use the key from your foreach, and unset the variable directly (from the session):
foreach ($_SESSION['cart'] as $key => $arrays3) {
if($arrays3['id'] == $id){
unset($_SESSION['cart'][$key]);
}
}
Unsetting $arrays3 or any of its children will only be effective until the next iteration of the foreach loop, when it will be set again.
You are using a dangerous form of the foreach loop. You MUST always unset the reference variable after the loop:
foreach ($_SESSION['cart'] as &$arrays3) {}
unset($arrays3);
Otherwise things will break if that loop is used again.
And the reference really is not needed. foreach operates on a copy of the array, so changes to the key or value will not go back into the original array, but you can always access the original array, as shown in the answer of @scrowler.
I know this post is old, but I think this info is very important: You asked:
Is it necessary to unset($value), when $value remains after foreach loop, is it good practice?
It depends on wheter you iterate by value or by reference.
First case (by value):
$array = array(1, 2, 3, 4);
foreach ($array as $value) {
//here $value is 1, then 2, then 3, then 4
}
//here $value will continue having a value of 4
//but if you change it, nothing happens with your array
$value = 'boom!';
//your array doesn't change.
So it's not necessary to unset $value
Second case (by reference):
$array = array(1, 2, 3, 4);
foreach ($array as &$value) {
//here $value is not a number, but a REFERENCE.
//that means, it will NOT be 1, then 2, then 3, then 4
//$value will be $array[0], then $array[1], etc
}
//here $value it's not 4, it's $array[3], it will remain as a reference
//so, if you change $value, $array[3] will change
$value = 'boom!'; //this will do: $array[3] = 'boom';
print_r ($array); //Array ( [0] => 1 [1] => 2 [2] => 3 [3] => boom! )
So in this case it is a good practice to unset $value, because by doing that, you break the reference to the array. Is it necessary? No, but if you don't do it, you should be very careful.
It can lead to unexpected results like this one:
$letters = array('a', 'b', 'c', 'd');
foreach ($letters as &$item) {}
foreach ($letters as $item) {}
print_r ($letters); //output: Array ( [0] => a [1] => b [2] => c [3] => c )
// [a,b,c,c], not [a,b,c,d]
This is also valid for PHP 7.0
Same code with unset:
$letters = array('a', 'b', 'c', 'd');
foreach ($letters as &$item) {}
unset ($item);
foreach ($letters as $item) {}
print_r ($letters); //output: Array ( [0] => a [1] => b [2] => c [3] => d )
// now it's [a,b,c,d]
I hope this can be useful for someone in the future. :)
There's no need to use unset in the current context that you are using it. unset will simply destroy the variable and its content.
In the example you are giving, this is looping through an array creating $value, then you are unsetting that variable. Which means it no longer exists in that code. So that does absolutely nothing.
To visuallize what I am talking about look at this example:
$value = 'Hello World';
echo $value;
unset($value);
echo $value;
The following out will be:
Hello World<br /><b>NOTICE</b> Undefined variable: value on line number 6<br />
So you will first see the Hello World, but after unsetting that variable trying to call it will just cause an error.
To answer your question, you really don't have to unset value; there's no need for it. As the foreach loop is setting a $value of each array() + 10.
Unsetting it will cause the work to be removed, and forgotten.
In my opinion, the best way to remove array elements based on indexes is to use the array_* set of functions, like array_diff and array_intersect (or array_diff_key and array_intersect_key in your situation).
$indexes_to_remove = array(2,3,4);
$indexes_to_remove = array_flip($indexes_to_remove);
$array = array_diff_key($array,$indexes_to_remove);
If the array is guaranteed to be exhausted at some point, you can use this:
while (true) {
$searchresult[] = search function returns various other keys from array
foreach($searchresult as $deletionid) {
unset($array[$deletionid]);
}
if (count($array) === 0) {
break;
}
}
And yes I know while (true) is pretty evil, but I find in cases like these it does exactly what is needed.
If you want to prevent it from infinite looping you could always add a variable, increment each iteration, and break when it reaches a high value that should never happen (like 10 * count($array))