Get Array Element from Class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Get Array Element from Class

Post by hawleyjr »

Hello,

I have a class as such:

Code: Select all

class MyClass{ 
    
    var $a_myArray=array(); 
    
    function MyClass(){ 
    
     $this->a_myArray = array('a','b','c');

    } 
    
    function getA_MyArray(){ return $this->a_myArray; } 
    function setA_MyArray($x){ $this->a_myArray=$x; } 
    
}
In my php code I would like to access the array variable like such but am unable.

Code: Select all

$Mc = new MyClass();

for($x=0;$x<count($Mc->getA_MyArray());$x++)&#123;

echo $Mc->getA_MyArray()&#1111;$x];

&#125;
This can be accomplished by something like:

Code: Select all

$Mc = new MyClass();

$myArray = $Mc->getA_MyArray();

for($x=0;$x<count($myArray);$x++)&#123;

echo $myArray&#1111;$x];

&#125;
Can I do this without creating a new array?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

I don't know what you're trying to accomplish with this code snippet, but if you would like to speed it up by a few fractions of a second :) I would recommend that you not make a call to the function count() each iteration of the for loop. Instead, you might try storing the same value in a variable and then comparing $x to that variable.

Like:

Code: Select all

$totalElements = count($Mc->getA_MyArray());
for ($x=0;$x<$totalElements;$x++) {
  // More code
}
As for the question that you actually wanted answered, I can't help, sorry :(

But hope this post has been of some benefit?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Yes, you are correct about not making the count() call. I was just giving an example.

The reason for this post is because my class actually looks something like this:

Code: Select all

class MyClass&#123; 
   
    var $a_myArray1=array(); 
    var $a_myArray2=array(); 
    var $a_myArray3=array(); 
    var $a_myArray4=array(); 
    var $a_myArray5=array(); 
    
    function MyClass()&#123; 
    
         $qry = ""; 
    
         $x=0; 
         $result = mysql_query($qry); 
         while($row = mysql_fetch_array($result)) 
         &#123; 
            $this->a_myArray1&#1111;$x] = $row&#1111;"myArray1"]; 
            $this->a_myArray2&#1111;$x] = $row&#1111;"myArray2"]; 
            $this->a_myArray3&#1111;$x] = $row&#1111;"myArray3"]; 
            $this->a_myArray4&#1111;$x] = $row&#1111;"myArray4"]; 
            $this->a_myArray5&#1111;$x] = $row&#1111;"myArray5"]; 

            $x++; 
         &#125; 
    &#125; 
    
    function getA_MyArray1()&#123; return $this->a_myArray1; &#125; 
    function getA_MyArray2()&#123; return $this->a_myArray2; &#125; 
    function getA_MyArray3()&#123; return $this->a_myArray3; &#125; 
    function getA_MyArray4()&#123; return $this->a_myArray4; &#125; 
    function getA_MyArray5()&#123; return $this->a_myArray5; &#125; 

    
    function setA_MyArray1($x)&#123; $this->a_myArray1=$x; &#125; 
    function setA_MyArray2($x)&#123; $this->a_myArray2=$x; &#125; 
    function setA_MyArray3($x)&#123; $this->a_myArray3=$x; &#125; 
    function setA_MyArray4($x)&#123; $this->a_myArray4=$x; &#125; 
    function setA_MyArray5($x)&#123; $this->a_myArray5=$x; &#125; 

    
&#125;
But with about 25 variables. I'd rather make a call to the class variable then create a new array.

Any suggestions?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

To answer my own question...

Code: Select all

class MyClass&#123; 
    
    var $a_myArray=array(); 
    
    function MyClass()&#123; 
    
     $this->a_myArray = array('a','b','c'); 

    &#125; 
    
    function getA_MyArray($x)&#123; return $this->a_myArray&#1111;$x]; &#125; 
    
&#125;

Code: Select all

$Mc = new MyClass(); 

for($x=0;$x<count($Mc->getA_MyArray());$x++)&#123; 

echo $Mc->getA_MyArray($x); 

&#125;
Post Reply