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

Arrays : returning a reference to an array item ??

Hello folks,

I am currently storing a set of objects inside an array,

$itemlist = array();
$itemlist[] = new item("myitem");
//...

and I am looking to develop a search function, which returns a
reference to the found item.

function &search_item($itemlist,"myitem") {
//...
}

I am trying to figure out how to obtain a reference to a given item in
an array.
Indeed, foreach() and each() seem to work on copies of the data in the
array.
For example:

$array = array("a","b","c","d");
print_r($array);
foreach($array as $v) $v="xxx";
print_r($array); // $array is unchanged

reset($array) ; while(list(,$v)=each($array)) $v="xxx";
print_r($array); // $array is unchanged, again

Would you have any idea how to achieve what I am looking for ?
Thanks in advance,
--
NZ
Jul 17 '05 #1
5 2849
On Wed, 11 Aug 2004 06:58:47 -0700, Nico wrote:
Hello folks,

I am currently storing a set of objects inside an array,

$itemlist = array();
$itemlist[] = new item("myitem");
//...

and I am looking to develop a search function, which returns a
reference to the found item.

function &search_item($itemlist,"myitem") {
//...
}

I am trying to figure out how to obtain a reference to a given item in
an array.
Indeed, foreach() and each() seem to work on copies of the data in the
array.
For example:

$array = array("a","b","c","d");
print_r($array);
foreach($array as $v) $v="xxx";
print_r($array); // $array is unchanged

reset($array) ; while(list(,$v)=each($array)) $v="xxx";
print_r($array); // $array is unchanged, again

Would you have any idea how to achieve what I am looking for ?
Thanks in advance,

for ($i = 0; $i < sizeof($array); $i++) {
$array[$i] = 'xxx';
}
This sort of what you're after?

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #2
Ian.H wrote:
On Wed, 11 Aug 2004 06:58:47 -0700, Nico wrote:
Hello folks,

I am currently storing a set of objects inside an array,

$itemlist = array();
$itemlist[] = new item("myitem");
//...

and I am looking to develop a search function, which returns a
reference to the found item.

function &search_item($itemlist,"myitem") {
//...
}

I am trying to figure out how to obtain a reference to a given item
in an array.
Indeed, foreach() and each() seem to work on copies of the data in
the array.
For example:

$array = array("a","b","c","d");
print_r($array);
foreach($array as $v) $v="xxx";
print_r($array); // $array is unchanged

reset($array) ; while(list(,$v)=each($array)) $v="xxx";
print_r($array); // $array is unchanged, again

Would you have any idea how to achieve what I am looking for ?
Thanks in advance,


Thank you for your reply,
for ($i = 0; $i < sizeof($array); $i++) {
$array[$i] = 'xxx';
}
This sort of what you're after?


Ok that does it, but this is not my point...
My point is : the examples I gave are just meant to show that foreach(), as
well as list(), make a COPY of the data stored in the array ; modifying the
data does not modify the array content.
What I am looking for is a way to have a REFERENCE on the array item's data,
in order to manipulate this data.

For example:

class foo {
var $name;
var $value;
// etc...
function foo($name, $value) { $this->name = $name ; $this->value=$value; }
}

class foo_bunch {
var $foos=array();
function new_foo($name, $value) {
$foos[] = new foo($name,$value);
}
// search should return a reference on the foo with specified name
function &search($name) {
// go through the $foos[] array, find the name, return a reference
on the foo
foreach($this->foos as $foo) // here $foo is a copy of the array's
item
if($foo->name==$name) return($foo);
return(NULL);
}
}

$my_bunch = new foo_bunch();
$my_bunch->new_foo("lemon","yellow");
$my_bunch->new_foo("tomato","red");
$my_bunch->new_foo("apple","red");
$foo =& $my_bunch->search("apple");
$foo->value = "green";

The point is : return a reference to the array's item (foo), to do whatever
with the data included therein...
Is it clearer this way ?

TIA
--
Nico | http://nzeches.free.fr
Un intellectuel assis va moins loin qu'un con qui marche. (M.Audiard)
Jul 17 '05 #3
On Thu, 12 Aug 2004 00:38:50 +0200, Nico wrote:
Ian.H wrote:
On Wed, 11 Aug 2004 06:58:47 -0700, Nico wrote:
Hello folks,

I am currently storing a set of objects inside an array,

$itemlist = array();
$itemlist[] = new item("myitem");
//...

and I am looking to develop a search function, which returns a
reference to the found item.

function &search_item($itemlist,"myitem") {
//...
}

I am trying to figure out how to obtain a reference to a given item
in an array.
Indeed, foreach() and each() seem to work on copies of the data in
the array.
For example:

$array = array("a","b","c","d");
print_r($array);
foreach($array as $v) $v="xxx";
print_r($array); // $array is unchanged

reset($array) ; while(list(,$v)=each($array)) $v="xxx";
print_r($array); // $array is unchanged, again

Would you have any idea how to achieve what I am looking for ?
Thanks in advance,


Thank you for your reply,

for ($i = 0; $i < sizeof($array); $i++) {
$array[$i] = 'xxx';
}
This sort of what you're after?


Ok that does it, but this is not my point...
My point is : the examples I gave are just meant to show that foreach(), as
well as list(), make a COPY of the data stored in the array ; modifying the
data does not modify the array content.
What I am looking for is a way to have a REFERENCE on the array item's data,
in order to manipulate this data.

For example:

class foo {
var $name;
var $value;
// etc...
function foo($name, $value) { $this->name = $name ; $this->value=$value; }
}

class foo_bunch {
var $foos=array();
function new_foo($name, $value) {
$foos[] = new foo($name,$value);
}
// search should return a reference on the foo with specified name
function &search($name) {
// go through the $foos[] array, find the name, return a reference
on the foo
foreach($this->foos as $foo) // here $foo is a copy of the array's
item
if($foo->name==$name) return($foo);
return(NULL);
}
}

$my_bunch = new foo_bunch();
$my_bunch->new_foo("lemon","yellow");
$my_bunch->new_foo("tomato","red");
$my_bunch->new_foo("apple","red");
$foo =& $my_bunch->search("apple");
$foo->value = "green";

The point is : return a reference to the array's item (foo), to do whatever
with the data included therein...
Is it clearer this way ?

TIA

Sorry Nico.. I guess I took your actual example code a little too literal
rather than the actual issue =)

I understand the issue (thought about this a few times myself which is
what immediately made me think of the for() loop). I'm not sure however,
if I actually have a decent answer to give on how / why.

Sorry I can't shed more light on the problem atm but wanted to acknowledge
your reply =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #4
.oO(Nico)
The point is : return a reference to the array's item (foo), to do whatever
with the data included therein...
Is it clearer this way ?


<?php
function &searchItem(&$itemList, $name) {
foreach($itemList as $key => $item) {
if ($item['name'] == $name) {
return $itemList[$key];
}
}
return NULL;
}

$foo = array(
array('name' => 'apple', 'color' => 'green'),
array('name' => 'lemon', 'color' => 'yellow')
);

header('Content-type: text/plain');
print_r($foo);
$bar = &searchItem($foo, 'lemon');
$bar['color'] = 'blue';
print_r($foo);
?>
Notice the used '&':
1) The argument $itemList is passed by reference.
2) The search function returns a reference.
3) Assigning the return value to a variable also needs a reference
operator.

HTH
Micha
Jul 17 '05 #5
Michael Fesser wrote:
<?php
function &searchItem(&$itemList, $name) {
foreach($itemList as $key => $item) {
if ($item['name'] == $name) {
return $itemList[$key];
}
}
return NULL;
}
HTH
Micha


This sure helped a lot !
Simple enough, just go back to the source, and forget about the $item, which
is a copy of the array's item
It works great, many thanks...
--
Nico | http://nzeches.free.fr
Un intellectuel assis va moins loin qu'un con qui marche. (M.Audiard)
Jul 17 '05 #6

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

Similar topics

7
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
18
by: Mike Bartels | last post by:
Hi Everyone! I have two Arrays A and B. Both arrays are byte arrays with 7 bytes each. The contents of array A and B are the same A = {1, 2, 3, 4, 5, 6, 7}; B = {1, 2, 3, 4, 5, 6, 7}; When...
3
by: parrot toes | last post by:
Summary: I have been trying to make requests of a web service provided by Axis using a dotnet client with code generated by wsdl.exe and have been getting exceptions when trying to process the...
4
by: mom_newbie | last post by:
Hello all, I want to perform the following task: 1. Create an array 2. Fill it up (number of elements unknown in advance) 3. Iterate through it using For Each loop (cannot do this in the in...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
8
by: jodleren | last post by:
Hi! I have a function, a part of my code which I can use as a function. It will return 2 arrays, and I am wondering what way to do so. Both arrays hold strings, there are no special keys. 1)...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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,...

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.