473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Array of objects

Hi! I'm a C++ programer that just recently started writing some scripts in
php. I don't understand why the following code doesn't word:

for($i = 0; $i < 10; $i++)
{
$bar.$title = "Bar";
$bar.$index = $i;

$bars[] = $bar;
}

foreach($bars as $bar)
echo($bar.$title . $bar.$index . "<br>");

It prints Bar9 ten times. Why? How to get it to work properly?

Using php version 4.3.9. Thanks!
Jul 17 '05 #1
3 1502
MRB
Marko wrote:
Hi! I'm a C++ programer that just recently started writing some scripts in
php. I don't understand why the following code doesn't word:

for($i = 0; $i < 10; $i++)
{
$bar.$title = "Bar";
$bar.$index = $i;

$bars[] = $bar;
}

foreach($bars as $bar)
echo($bar.$title . $bar.$index . "<br>");

It prints Bar9 ten times. Why? How to get it to work properly?

Using php version 4.3.9. Thanks!

You're using the wrong syntax, use the following to access objects members:

$bar->title = "bar" ;
$bar->index = $i;
Cheers
Jul 17 '05 #2
> You're using the wrong syntax

You're right! I changed it and everything is working properly. Thanks!
Jul 17 '05 #3
On Thu, 05 May 2005 14:21:24 +0200, Marko wrote:
foreach($bars as $bar)
echo($bar.$title . $bar.$index . "<br>");

It prints Bar9 ten times. Why? How to get it to work properly?

Using php version 4.3.9. Thanks!


The "." is string concatenation, not member reference. That would be "->".
Also, you are assigning to the same variable bar. Your code will not work
because objects are references and you are always trying to use the same
object. That will set the whole array to the same value. You would
probably want something like this:

#!/usr/local/bin/php
<?php
class cls {
public $title = "";
public $index = "";
}
for ($i = 0;$i<10;$i++) {
$bar = new cls();
$bar->title = "Bar";
$bar->index = $i;
$bars[] = $bar;
}
foreach($bars as $bar) echo ($bar->title.$bar->index."\n");
?>

If I understood you correctly, this is what you want:
$ ./ttt.php
Bar0
Bar1
Bar2
Bar3
Bar4
Bar5
Bar6
Bar7
Bar8
Bar9

Unfortunately, I am no longer using PHP4 and I don't have it on the system.

--
Egoist: A person of low taste, more interested in themselves than in me.

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
2
by: James | last post by:
Hi, I'm hoping someone can help me out. If I declare a class, eg. class CSomeclass { public: var/func etc..... private varfunc etc..
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.