How to read (or adjust) a single entry of an object wich comes from an array



English version:
Hi
In a simple testcode i made a few objects from an array.
This works fine when i read the objects with a foreach statement. (see code
below)

But when i trie to read a single object from this array, i get an
errormessage that an unexpected [ exists on line x
if($objCar[1]->$this->m_sColor=="Blue")

I searched the internet and found something that could bring the solution.
Place the wanted object in a variable first and than read it.
$oCar=$objCar[1];
if($oCar->$this->m_sColor=="Blue")
I now get an errormessage Fatal error: Cannot use object of type clsCar as
array in /cursusaccounts/twintershovedu/site/OOAuto1.php on line 60

My question is:
How can i read a single object wich is created from an array so that the
code below does work.

------------------ The
Code ----------------------------------------------------
$aCar = array();

$aCar[0] = new clsCar("Mercedes", "A180", "Beige");
$aCar[1] = new clsCar("Toyota", "Avensis", "Blauw");
$aCar[2] = new clsCar("Volvo", "440", "Wit");

foreach($aCar as $objCar)
{
$objCar->printCarProperties();
}

//Till this point all works fine..... but then......

$oCar=$objCar[1];

if($oCar->$this->m_sColor=="Blue")
{
echo"<br>Yes... This car is blue";
}
----------------------------------------------------------------------------
---------
Thanks in advanced
Tino Wintershoven
The Netherlands


Dutch version:
Hallo,

Ik heb als oefeningetje een array gemaakt van een bepaalde class.
Op zich werkt dit prima en alles wordt in de foreach keurig uitgelezen.

Als ik echter een enkele entry uit de array wil benaderen krijg ik een
foutmelding dat er een "unexpected" blokhaak [ aanwezig is.
if($objAuto[1]->$this->m_sKleur=="Blauw")

Ik heb gezocht op Internet en vond iets dat wellicht de oplossing kon
bieden.
Zet het object eerst in een variabele en lees het dan uit.
$oAuto=$objAuto[1];
if($oAuto->$this->m_sKleur=="Blauw")

Nu krijg ik echter weer een foutmelding:
Fatal error: Cannot use object of type clsAuto as array in
/cursusaccounts/twintershovedu/site/OOAuto1.php on line 60

Mijn vraag is dus:
Hoe kan je een enkel object uitlezen (of bewerken) als deze gemaakt is
middels een array zodat onderstaande code wel werkt?

Bij voorbaat dank
Tino Wintershoven.


------------------ De
Code ----------------------------------------------------
$aAuto = array();

$aAuto[0] = new clsAuto("Mercedes", "A180", "Beige");
$aAuto[1] = new clsAuto("Toyota", "Avensis", "Blauw");
$aAuto[2] = new clsAuto("Volvo", "440", "Wit");

foreach($aAuto as $objAuto)
{
$objAuto->printAutoKenmerken();
}

//Tot hier werkt het prima.... maar dan...

$oAuto=$objAuto[1];

if($oAuto->$this->m_sKleur=="Blauw")
{
echo"<br>Ja... deze auto is blauw";
}


.



Relevant Pages

  • Re: foreach issue
    ... It is read-only just to prevent you for making mistakes. ... but it won't affect the reference kept in the collection. ... the collection (array or non-typed collection) and what is the type of the ... > When doing a foreach statement like the following why is the variable ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to read (or adjust) a single entry of an object wich comes from an array
    ... In a simple testcode i made a few objects from an array. ... This works fine when i read the objects with a foreach statement. ... Place the wanted object in a variable first and than read it. ... Your not in the foreach loop anymore so $objCar is left as equal to $aCarat that point. ...
    (alt.php)
  • PHP form arrays
    ... the page that sorts the array and updates the database. ... do I need to set up the HTML form in a special way ... I would like to use the foreach statement, but am not too sure how it ... I get the text: 0 Array ...
    (php.general)
  • Re: foreach doesnt work with array of bools?
    ... Exactly what the compiler says. ... The variable in a foreach statement is ... When you create a new array, ... Please reply only to the newsgroup. ...
    (microsoft.public.dotnet.languages.csharp)
  • Array of Arrays in VBA
    ... Onderstaande werkt NIET maar geeft wel de idee weer waar het heen ... dan fouten gegenereerd op de dimensies e.d. ... ook kunnen voor een ééndimensionele dynamische array of kan dit gewoon ... een alternatieve oplossing of benadering voor een dergelijk ...
    (microsoft.public.excel.programming)