Jump to content


Photo

Array Manipulation


  • Please log in to reply
4 replies to this topic

#1 spelltwister

spelltwister
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 11 March 2006 - 09:04 PM

Hello everyone!

I was wondering, what i'm doing wrong with this code:

<?php

$colors = array('red','green','blue');

unset($colors[1]);

for($i = 0;$i<sizeof($colors);$i++){

echo $colors[$i];

}

?>

Why does blue disappear too?

Also, what I was trying to do with that is remove just green and have the array size become 2 (0 and 1 have elements) so that i could randomly select from a larger list without hitting a blank. IE

<?php

$colors = array('red','green','blue','orange','yellow','purple');

for($i=0;$i<6;$i++){
$rand = rand(0,sizeof($colors)-1);

echo $colors[$rand];

unset($colors[$rand]);
}

?>

Any ideas?

Here's what I really want to do:

$defense_infantry = array( "nation" => array(), "number" => array() );

$upper = sizeof($offense_infantry["nation"]);
        $index = rand(0,$upper-1);
        if($offense_infantry["number"][$index] > 0){
           $attInfantry--;
           $offense_infantry["number"][$index] -= 1;
           $k+=1;
           echo 'Attacker lost an infantry to artillery</br>';
        }else{
          echo 'empty attacker infantry to artillery</br>'; <== I want to get rid of this posibility
        }
        if($offense_infantry["number"][$index] ==0){
          unset($offense_infantry["number"][$index]);
          unset($offense_infantry["nation"][$index]);
        }

Thanks

Mike

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 11 March 2006 - 09:16 PM

$colors = array('red','green','blue');

unset($colors[1]);

for($i = 0;$i<sizeof($colors);$i++){

        echo $colors[$i];

}

"blue" is still there but its key remains 2 so you don't echo it. Try

$colors = array('red','green','blue');

unset($colors[1]);

foreach ($colors as $c) {

         echo $c;

}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 spelltwister

spelltwister
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 11 March 2006 - 10:02 PM

so, you are saying that the sizeof is saying that it is only two elements but the keys remain the same. interesting. Any idea how to shift all the keys down? What i really want to do with it is in the last example. Thanks for the help though

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 12 March 2006 - 12:15 AM

you could

$colors = array('red','green','blue');
unset($colors[1]);
$colors = array_values($colors);

giving-->

Array
(
    [0] => red
    [1] => blue
)

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 spelltwister

spelltwister
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 12 March 2006 - 01:07 AM

you are amazing. any chace you would know how to apply it to the multi-dimensional array that i have? would be it like this:

$offense_infantry = array_values($offense_infantry);

so that each of the arrays in the array smash together?

or would it be like this:

$offense_infantry["nation"] = array_values($offense_infantry["nation"]);
$offense_infantry["number"] = array_values($offense_infantry["number"]);

Thanks so much. You rule




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users