473,394 Members | 1,674 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,394 software developers and data experts.

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 bandingCalculator($properties){

$iterator = $properties->getIterator();

while ( $iterator->valid() ){
if ( 0 == strcmp($iterator->current()-
>TAX_CODE,"GF") ){
$iterator->current()->BAND_ASSESSMENT = 666;
$iterator->current()->['newProperty'] = 1234;
}
$iterator->next();
}
}
Aug 6 '08 #1
1 6782
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 bandingCalculator($properties){
$iterator = $properties->getMyIterator();

while ( $iterator->valid() ){
if (is_object($iterator->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_ASSESSMENT = 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($iterator->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($iterator->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;

bandingCalculator($properties);

?>
Aug 6 '08 #2

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

Similar topics

83
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...
6
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 =...
16
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...
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);
5
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>...
2
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...
9
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...
30
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
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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...

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.