Re: Arrays : returning a reference to an array item ??
From: Nico (nzeches_Beurk_at_LeSpam_free.fr)
Date: 08/12/04
- Next message: Michael Fesser: "Re: Upgraded php, lost php file association"
- Previous message: Dominik Jain: "unicode form data - how to obtain character codes?"
- In reply to:(deleted message) Ian.H: "Re: Arrays : returning a reference to an array item ??"
- Next in thread: Ian.H: "Re: Arrays : returning a reference to an array item ??"
- Reply:(deleted message) Ian.H: "Re: Arrays : returning a reference to an array item ??"
- Reply: Michael Fesser: "Re: Arrays : returning a reference to an array item ??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 12 Aug 2004 00:38:50 +0200
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)
- Next message: Michael Fesser: "Re: Upgraded php, lost php file association"
- Previous message: Dominik Jain: "unicode form data - how to obtain character codes?"
- In reply to:(deleted message) Ian.H: "Re: Arrays : returning a reference to an array item ??"
- Next in thread: Ian.H: "Re: Arrays : returning a reference to an array item ??"
- Reply:(deleted message) Ian.H: "Re: Arrays : returning a reference to an array item ??"
- Reply: Michael Fesser: "Re: Arrays : returning a reference to an array item ??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|