Jump to content

Adding to an array


b1011

Recommended Posts

You know how you can do this:

array("#ff0000","#ff8800","#0022ff","#33ff33")

 

Well how could i set it up so that in a while statement it adds to the array? And if its possible how can i make it add a random color (#ff0000 etc.);

Any ideas?

Did i explain good enough?

 

 

Link to comment
Share on other sites

<?php 
$colors = array('#ffff23', '#faaf23', '#ff3323', '#f99f23', '#ff8823', '#fff773'); 
?>

 

Then you call it by $colors[position] the position is the spot ur value is in so if u want #ffff23 it would be $colors[0]

Link to comment
Share on other sites

Adding to the array is as simple as doing $array[] = $var. There's a few examples in the user comments on the php site for the mt_rand function for random hex codes. Ive used one for this example:

 

<?php
$array = array("#ff0000","#ff8800","#0022ff","#33ff33");
for($x=0;$x<9;$x++){
    //thanks to zolaar at nothanks dot com - taken from the a comment on http://uk3.php.net/mt_rand
   $num = mt_rand ( 0, 0xffffff ); 
   $output = sprintf ( "%06x" , $num ); 
   $array[] = '#'.$output;
}
print_r($array);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.