1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do I access this array?

Discussion in 'PHP' started by papa_face, Feb 22, 2007.

Thread Status:
Not open for further replies.
  1. #1
    Hello,
    I need to know how to access the array so i can find a certain key and its value e.g. $array[4];
    The code i have is:
    example.php
    <?php
    /* GRADIENT CLASS EXAMPLE */
    require "class.gradient.php";
    
    $fade_from = "#FFffff"; // HEX-Color to fade from
    $fade_to = "#000000";   // HEX-Color to fade to
    $steps = 15;                        // number of steps creating the gradient
    
    $gradient = new gradient($fade_from,$fade_to,$steps);         // define the class
    
    echo "Gradient from <b>".$gradient->color1."</b> to <b>".$gradient->color2."</b></p><pre>";
    
    print_r($gradient->createArray());                                                 // outputs the array to see the values
    
    echo "</pre><table width='300' border='0'>";                            // creates the table with colors
      foreach($gradient->createArray() AS $color){
          echo "<tr><td bgcolor=$color>&nbsp;</td><td>$color</td></tr>";
        };
      echo "</table>";
    ?>
    Code (markup):
    class.gradient.php
    <?php
    
    class gradient
    {
                var $color1 ;
          var $color2 ;
          var $n = 10;
          var $c1codes;
          var $c2codes;
    
          
          /* constructor */
          function gradient($color1,$color2,$n){
                  $this->color1 = $color1;
              $this->color2 = $color2;
              $this->n = $n;
              $this->c1codes=$this->getHexValues($this->color1);
                  $this->c2codes=$this->getHexValues($this->color2);
            }
         
         /* internal: do not call */   
         function getHexValues($color) {
                    $color = substr($color, 1);
                    return array(hexdec(substr($color,0,2)),hexdec(substr($color,2,2)),hexdec(substr($color,4,2)));    
                    }
            
         function createArray(){                
                      $red=($this->c2codes[0]-$this->c1codes[0])/($this->n-1);
                      $green=($this->c2codes[1]-$this->c1codes[1])/($this->n-1);
                      $blue=($this->c2codes[2]-$this->c1codes[2])/($this->n-1);                  
                      
                      for($i=0;$i<$this->n;$i++){
                                  $newred=dechex($this->c1codes[0]+round($i*$red));
                                if(strlen($newred)<2) $newred="0".$newred;
                                  
                                $newgreen=dechex($this->c1codes[1]+round($i*$green));
                                if(strlen($newgreen)<2) $newgreen="0".$newgreen;
                               
                                   $newblue=dechex($this->c1codes[2]+round($i*$blue));
                                if(strlen($newblue)<2) $newblue="0".$newblue;
                                
                                $return[$i]="#".$newred.$newgreen.$newblue;
                       }
                      
                      return $return;
                  }
    }
    ?> 
    Code (markup):
    In example.php you will see $gradient->createArray())
    How do get a hold of that array that it creates so i can get a value depending on the key i want?

    Any help would be appreciated.
     
    papa_face, Feb 22, 2007 IP
  2. lmoss

    lmoss Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this:

    $my_array = $gradient->createArray();
    $my_value = $my_array[4];
    // do stuff with $my_value
    
    Code (markup):
     
    lmoss, Feb 22, 2007 IP
  3. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #3
    Works perfectly, thank you!
     
    papa_face, Feb 22, 2007 IP
  4. disgust

    disgust Guest

    Messages:
    2,417
    Likes Received:
    133
    Best Answers:
    0
    Trophy Points:
    0
    #4
    get_defined_vars & var_dump :)

    edit: oops, too late. oh well !

    still, handy to know ;)
     
    disgust, Feb 22, 2007 IP
Thread Status:
Not open for further replies.