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



On 2006-08-26 03:37:14 -0600, "T. Wintershoven" <twintershoven@xxxxxxxxx> said:

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

Change

$oCar=$objCar[1];

to

$oCar=$aCar[1];

Your not in the foreach loop anymore so $objCar is left as equal to $aCar[2] at that point. Your statement says in essence $oCar=$aCar[2][1]; I assume $aCar[2][1] is invalid. Then change

if($oCar->$this->m_sColor=="Blue")

to

if($oCar->m_sColor=="Blue)

unless your $this variable has as a property name in it at that point. If it does you could do

if ($oCar->$$this->m_sColor=="Blue")

where $this == the name of an internal object...

I don't really see why if($aCar[1]->m_sColor=="Blue") couldn't work there as instead of all that.

.



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)
  • 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. ... Op zich werkt dit prima en alles wordt in de foreach keurig uitgelezen. ... Zet het object eerst in een variabele en lees het dan uit. ...
    (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)