Jump to content

How do I access this array?


papaface

Recommended Posts

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> </td><td>$color</td></tr>";
    };
  echo "</table>";
?>

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;
              }
}
?> 

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.

Link to comment
https://forums.phpfreaks.com/topic/39664-how-do-i-access-this-array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.