0

This is my code:

    $pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pizza2 = explode(" ",$pizza);

//echo count($pizza2);

for($i = 0; $i<count($pizza2); $i++)
{//echo "<b>$i</b>";

    echo "Array #".$i." is: <b>".$pizza2[$i]. "</b> ";
    unset($pizza2[$i]);
    }

But the output I get is only till piece3... why is that? I want output till piece6

6 Answers 6

2

This is because you are doing count($pizza2) in forloop and once unset the value its length is also decreasing on each loop.

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pizza2 = explode(" ",$pizza);
//echo count($pizza2);
$len = count($pizza2);
for($i = 0; $i<$len; $i++)
{//echo "<b>$i</b>";

    echo "Array #".$i." is: <b>".$pizza2[$i]. "</b> ";
    unset($pizza2[$i]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure I understand what you are saying... I know I am unsetting it after displaying it, but why is it only displaying the first three?
ok.. inside the loop you are doing unset() one element so in first time it gets count as 6.. then after the unset the array length becomes 5, then unset len now is 4.. by the same time i is also incremented and it shows 3 elements and after that $i becomes greater then the array length because of successive unset and loop breaks.
Yep, got it now from the answers, thanks! Voted you up!
1

This is a logical issue.

This line:

 unset($pizza2[$i]);

decreases the length of your array every time you use unset. So in the fourth iteration it has array count==3 and $i==3, so it exits from the loop.

You need to get the count into a variable before you unset arrays, and use the variable in the loop.

Like this:

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pizza2 = explode(" ",$pizza);
print_r($pizza2 );
$len = count($pizza2);

for($i = 0; $i<$len; $i++)
{
//echo "<b>$i</b>";

    echo "Array #".$i." is: <b>".$pizza2[$i]. "</b> ";
    unset($pizza2[$i]);
    }

Comments

1

The reason the loop stops is that the count() of the array $pizza2 changes each time the loop runs. So the conditions of your for statement are changing on each loop, and when it reaches 3, your array is now shorter, so the condition is met and the loop stops. By the time $i has reached three, the array has been shortened to only three elements. This will demonstrate what's happening:

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pizza2 = explode(" ",$pizza);

for($i = 0; $i<count($pizza2); $i++)
{
    echo "Array #".$i." is: <b>".$pizza2[$i]. "</b> ";
    unset($pizza2[$i]);
    echo "pizza2 has ".count($pizza2)." elements";
}

To solve this, you could get the initial length of the array and store it:

$length = count($pizza2);
for($i = 0; $i<$length; $i++)
{//etc... as before

You could also simplify by using a foreach():

foreach ($pizza2 as $key => $value){
    echo "Array #" . $key . " is: " . $value ."<br/>";
}

Comments

1

This peice of code gives you your desired output just modified a single line

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pizza2 = explode(" ",$pizza);

//echo count($pizza2);
$Count = count($pizza2);
for($i = 0; $i<$Count; $i++)
{//echo "<b>$i</b>";

    echo "Array #".$i." is: <b>".$pizza2[$i]. "</b> ";
    unset($pizza2[$i]);
}

4 Comments

Nope, I have to display one piece, then unset it, then display the 2nd and then unset the 2nd... thats the exercise
made an edit ... see if this is what you want .. it woks too
and in your solution it was showing only three because your were decreasing the condition limit Which is the length of array every time you were unsetting something from array
Thanks, I got that from the answers :) Voted you up!
1

Use php foreach control structure, it is an easy way to iterate over arrays,

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pizza2 = explode(" ",$pizza);
foreach($pizza2 as $key=>$piece){
    echo "Array #".$key." is: <b>".$piece. "</b> ";
    unset($pizza2[$key]);
}

2 Comments

I have to unset each piece after I display it... thats one of the rules of the exercise
Updated Answer, Please check that!
1

You need to put count($pizza2) into some variable, ex: $count. It will keep the original size of your data, because unset($pizza2[$i]) will decrease the size of $pizza2.

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.