i want to create a for loop in order to unset the spesific variables in an array. i cant find any answer on the internet. here is my code.
$randomnumber=242;
$variables= array('var','var2','randomnumber');
for ($i = 0; $i < count($variables); $i++) {
unset($variables[$i]);
}
echo $randomnumber;
output is:
242
i dont know what am i missing. please help me guys. i want to unset "var1", "var2", and "randomnumber" variables in the array of "variables". output should be "undefined variable : $randomnumber" or smth like that.
Code
unset($variables[$i]);
means
unset value with key $i from array $variables
If you want to unset a variable with name $variables[$i] then you should use variable variable:
$randomnumber=242;
$variables= array('var','var2','randomnumber');
for ($i = 0; $i < count($variables); $i++) {
// variable variable syntax here
unset(${$variables[$i]});
}
echo $randomnumber;
simpler and faster solution:
$randomnumber = 242;
$variables = ['var', 'var2', 'randomnumber'];
foreach ($variables as $variableName) {
unset($$variableName);
}
Related
I've this these array values :
$cart_item['addons'][0]['price'] = '52';
$cart_item['addons'][1]['price'] = '34';
$cart_item['addons'][2]['price'] = '12';
......
....
I want that each values are at 0 like :
$cart_item['addons'][0]['price'] = '0';
$cart_item['addons'][1]['price'] = '0';
$cart_item['addons'][2]['price'] = '0';
....
...
So I try this code :
for ($i=0; $i > 0 ; $i++) {
$cart_item['addons'][$i]['price'] = '0';
}
But it does not work. Thanks for your help !
Try this simple solution:
$count=count($cart_item['addons']);
for($i=0; $i<$count;$i++ ){
$cart_item['addons'][$i]['price'] = '0';
}
If your array is big enough, putting count() function inside for loop is being a crazy coconut. It will be much, much slower. Please use the count outside the loop:
$count = count($cart_item['addons'])
for($i=0; $i<$count;$i++ ){
$cart_item['addons'][$i]['price'] = '0';
}
You have to loop more often to achive this:
foreach($cart_item['addons'] as &$addons {
foreach($addons as &$addon) {
$addon['price'] = 0;
}
}
You can iterate over $cart_item['addons'], like so:
foreach ($cart_item['addons'] AS $key => &$value) {
$value['price'] = 0;
}
(Your code does not get executed, because $i is never changed and so is never > 0)
Also note that you need to use the reference (&$value) when changing the array in foreach loop.
There are three parts to your for loop: for(counter | test | action){}. It might be useful for you to look at this guide about for loops. You initialise your variable:
$i = 0;
then you do a logical check (the test part):
$i > 0;
If we substitute the the variable ($i) for the value it holds (0) we get:
0 > 0
which will never be true and thus you will never get to the final part (action) of the for loop:
$i++;
Instead you could make it loop until it has moved through your entire array like this:
$elementCount = count(cart_item['addons']);
for($i=0; $i < $elementCount; $i++){
$cart_item['addons'][$i]['price'] = '0';
}
Each time it loops we add one to $i until we reach the stopping condition where $i is no longer less than the number of items in the array.
It's also worth noting that PHP has a number of functions that help working with arrays.
I have multiple PHP variables in the form
$number1, $number2, $number3 and so on...
I would like to dynamically reference these inside of a loop to retrieve information from them, but am not sure how to reference the static variable dynamically. Ex:
for($i = 1; $i <= 10; $i++) {
//The first number to be printed should be the value from
//$number1 not $number concatenated to $i
//here are some of the strings I tried:
echo "$number$i";
echo "{$number}$i";
echo "{$number}{$i}";
}
This should do it:
echo ${"number{$i}"};
But why not use arrays instead? Having $number[$i] is much more readable...
I have multiple PHP variables in the form
$number1, $number2, $number3 and so on...
I would like to dynamically reference these inside of a loop to retrieve information from them, but am not sure how to reference the static variable dynamically. Ex:
for($i = 1; $i <= 10; $i++) {
//The first number to be printed should be the value from
//$number1 not $number concatenated to $i
//here are some of the strings I tried:
echo "$number$i";
echo "{$number}$i";
echo "{$number}{$i}";
}
This should do it:
echo ${"number{$i}"};
But why not use arrays instead? Having $number[$i] is much more readable...
<?php
$listing_weblinkurl_1z1 = "some1";
$listing_weblinkurl_1z2 = "some2";
$listing_weblinkurl_1z3 = "some3";
$listing_weblinkurl_1z4 = "some4";
for($i=1;$i<=4;$i++){
print ($listing_weblinkurl_1z.$i);
}
?>
It is not working. I know that it is wrong to add $i in variable to call it. But I want it.
This is how it would be done, using variable variables.
for ( $i = 1; $i <= 4; $i+= 1 )
{
$varname = 'listing_weblinkurl_1z' . $i;
echo $$varname;
}
However, this is not a good way of writing code. Instead, $listing_weblinkurl should probably be an array containing keys of 1z1, 1z2, etc.
for($i=1;$i<=4;$i++) {
print ( ${"listing_weblinkurl_1z$i"} );
}
As #McAden pointed out in the comments, it would probably be a better idea to use an array.
I have an array declared above the beginning of a for loop as: $array = array();.
Now, in the for loop I start inserting values into it.
At some point I make one of its index as another array as $array[$j]=array();
And insert some values like, $array[$j][$l] = id; and so on.
Now, when I use print_r ($array); inside the loop I get the expected value of the array.
But outside the loop this newly created array (2-D) is getting lost and I am getting only a 1-D array as an output.
Can someone please tell me where the problem could lie?
The following code works properly. Perhaps you are switching your variables as strager suggests.
<?php
$array = array();
for ($i = 0; $i < 10; $i+=1) {
if ($i == 5) {
$array[$i] = array('value 1', 'value 2');
} else {
$array[$i] = $i;
}
}
print_r($array);
?>