Re: Arrays : returning a reference to an array item ??

From: Nico (nzeches_Beurk_at_LeSpam_free.fr)
Date: 08/12/04


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)


Relevant Pages

  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... changed if an array is passed by Val. ... ' new reference ... As each function creates a new array object, ... 'secondArray' and 'secondArrayCopy'. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Need help with textboxes
    ... The JavaScript 1.5 Reference already states: ... All forms and their children are stored in an array ... use dot notation or object literals. ... No. Bracket property accessors allow their argument to be any string value. ...
    (comp.lang.javascript)
  • Re: Garbage Collection Issues in long-standing services
    ... the pinned array should get unpinned or ... The receive case is done quite well, I do create a 4K buffer and reuse it ... and remove the reference to my wrapper socket class, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Inheritance of constructors.
    ... Bare in mind that was just for sake of example. ... In case of "reference by value" that's not ... Foo foo = new Foo; ... The changes to the array are visible in Foo, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to give selective access to the methods in a class?
    ... different memory locations at different times. ... The reference still leads to the ... So array resizing ... program pushes certain objects into the circular buffer, ...
    (comp.lang.java.programmer)