Question

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

Was it helpful?

Solution 2

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]);
    }

OTHER TIPS

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]);
}

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/>";
}

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]);
}

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]);
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top