1

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 !

3
  • 1
    $i > 0, infinite loop issue. Commented Jan 29, 2016 at 15:29
  • 3
    @RajdeepPaul technically it will make the loop never run because the first iteration is false. But yes, if that $i ever got above 0, infinite. Commented Jan 29, 2016 at 15:30
  • @JonathanKuhn Oh yes, missed that initialization part. Thanks for pointing that out. Commented Jan 29, 2016 at 15:34

5 Answers 5

4

Try this simple solution:

$count=count($cart_item['addons']);
for($i=0; $i<$count;$i++ ){
  $cart_item['addons'][$i]['price'] = '0';
}
Sign up to request clarification or add additional context in comments.

4 Comments

What if there are 5,000,000 elements in the array? You're going to count them on every iteration? That's insane.
@Nordenheim what do u suggest?
See my answer. Count can always be done outside the loop, obviously.
Great Updated in my answer.
2

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';
}

Comments

1

You have to loop more often to achive this:

foreach($cart_item['addons'] as &$addons {
    foreach($addons as &$addon) {
          $addon['price'] = 0;
    }
}

1 Comment

Just an FYI, avoid using references in foreach loops, especially if you plan on not unset'ing the reference. Variables are not scoped to the loop in PHP and so after the loop a reference to the last element in the array still exists. Using $addon again will make changes to that last value in the array. IMO, it is much easier to just foreach($a as $k=>$v) and reference the key in $k to modify the array. Avoids the issue altogether.
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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.