Page 1 of 1
Get Array Element from Class
Posted: Fri Mar 26, 2004 1:18 pm
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++){
echo $Mc->getA_MyArray()ї$x];
}
This can be accomplished by something like:
Code: Select all
$Mc = new MyClass();
$myArray = $Mc->getA_MyArray();
for($x=0;$x<count($myArray);$x++){
echo $myArrayї$x];
}
Can I do this without creating a new array?
Posted: Fri Mar 26, 2004 8:52 pm
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?
Posted: Sat Mar 27, 2004 9:48 am
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{
var $a_myArray1=array();
var $a_myArray2=array();
var $a_myArray3=array();
var $a_myArray4=array();
var $a_myArray5=array();
function MyClass(){
$qry = "";
$x=0;
$result = mysql_query($qry);
while($row = mysql_fetch_array($result))
{
$this->a_myArray1ї$x] = $rowї"myArray1"];
$this->a_myArray2ї$x] = $rowї"myArray2"];
$this->a_myArray3ї$x] = $rowї"myArray3"];
$this->a_myArray4ї$x] = $rowї"myArray4"];
$this->a_myArray5ї$x] = $rowї"myArray5"];
$x++;
}
}
function getA_MyArray1(){ return $this->a_myArray1; }
function getA_MyArray2(){ return $this->a_myArray2; }
function getA_MyArray3(){ return $this->a_myArray3; }
function getA_MyArray4(){ return $this->a_myArray4; }
function getA_MyArray5(){ return $this->a_myArray5; }
function setA_MyArray1($x){ $this->a_myArray1=$x; }
function setA_MyArray2($x){ $this->a_myArray2=$x; }
function setA_MyArray3($x){ $this->a_myArray3=$x; }
function setA_MyArray4($x){ $this->a_myArray4=$x; }
function setA_MyArray5($x){ $this->a_myArray5=$x; }
}
But with about 25 variables. I'd rather make a call to the class variable then create a new array.
Any suggestions?
Posted: Tue Mar 30, 2004 8:43 am
by hawleyjr
To answer my own question...
Code: Select all
class MyClass{
var $a_myArray=array();
function MyClass(){
$this->a_myArray = array('a','b','c');
}
function getA_MyArray($x){ return $this->a_myArrayї$x]; }
}
Code: Select all
$Mc = new MyClass();
for($x=0;$x<count($Mc->getA_MyArray());$x++){
echo $Mc->getA_MyArray($x);
}