473,769 Members | 4,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

add variable to array object at runtime

is it possible to add new variable to a array object at run time?
the following code does not work.
$iterator->current()->['newProperty'] = 1234;

how can this be done?

function bandingCalculat or($properties) {

$iterator = $properties->getIterator( );

while ( $iterator->valid() ){
if ( 0 == strcmp($iterato r->current()-
>TAX_CODE,"GF ") ){
$iterator->current()->BAND_ASSESSMEN T = 666;
$iterator->current()->['newProperty'] = 1234;
}
$iterator->next();
}
}
Aug 6 '08 #1
1 6813
On 6 aug, 19:16, BiraRai <bira...@gmail. comwrote:
is it possible to add new variable to a array object at run time?
the following code does not work.
$iterator->current()->['newProperty'] = 1234;
Here is some code where I did some assumptions about how your example
works and an explanation. Basically, you can change the return value
of current() if it is a reference, which is the case in objects, but
not when it is any other type.

Besides, you are trying to access the return value of current() as an
object in one line and as an array in the next. This is not possible.

You are probably better off not using an iterator, but an array and a
foreach or for loop, if you want to change arrays.

<?php

class MyIterator {
var $index = 0;
var $contents = array();

function current() {
return $this->contents[$this->index];
}

function next() {
$this->index++;
}

function valid() {
return $this->index < count($this->contents);
}
}

class Properties {
var $iterator = null;

function getMyIterator() {
return $this->iterator;
}
}

function bandingCalculat or($properties) {
$iterator = $properties->getMyIterator( );

while ( $iterator->valid() ){
if (is_object($ite rator->current())) {
/* This works. It adds BAND_ASSESSMENT to the current object. The
current() function actually returns a reference to an object, that you
can use to access the object. */
$iterator->current()->BAND_ASSESSMEN T = 666;
/* This does not work. It gives a parse error. Besides that,
current() returns an object and you try to access it as an array. */
// $iterator->current()->['newProperty'] = 1233;
}
if (is_array($iter ator->current())) {
/* An array is not an object. Instead of returning a reference,
current() now returns a copy of the array. You can change the copy,
but that will get you nowhere. You can change current() to
specifically return a reference, but that would not be the ideal way
to do this. */
$current = $iterator->current();
$current['newProperty'] = 1234;
}
print_r($iterat or->current());
$iterator->next();
}
}

$object = new stdClass();
$object->TAX_CODE = "GF";

$array = array(1, 2);

$iterator = new MyIterator();
$iterator->contents = array($object, $array);
$properties = new Properties();
$properties->iterator = $iterator;

bandingCalculat or($properties) ;

?>
Aug 6 '08 #2

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

Similar topics

83
6528
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
6
2751
by: BigDadyWeaver | last post by:
I am using the following code in asp to define a unique and unpredictable record ID in Access. <% 'GENERATE UNIQUE ID Function genguid() Dim Guid guid = server.createobject("scriptlet.typelib").guid guid=Left(guid,instr(guid,"}")) genguid=guid
16
25419
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
7
39848
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);
5
14114
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h> #include<stdlib.h> int main(void) { int y = { 7, 9,10},i; for (;i<20;i++)
2
1906
by: Hamish Symington | last post by:
Hello, I'm trying to re-code a site which I wrote in PHP into ASP.NET using Visual Basic .NET. Something PHP has which has proved invaluable is the concept of variable variables. I haven't had any success with working out how to do it and hoped someone might be able to help.
9
4098
by: mupe | last post by:
Hi, i have a problem with a Type Library, which is written in C++. I am developing an application in C#.NET and have to use functions from this COM-Type Library. When I use these functions in the "old" VB it works but not in .NET. I think it is a problem with marshalling but I could not find a solution yet. First I included the Type Library in VS.NET 2003. Other functions of the TL work so the reference to the Lib must be correct.
30
2948
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
17
7254
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
0
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10053
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10001
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9867
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8880
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.