Re: Arrays : returning a reference to an array item ??
From: Michael Fesser (netizen_at_gmx.net)
Date: 08/12/04
- Next message: steve: "UTF-8 not decoding"
- Previous message: Kristopher K. Kruger: "Re: Upgraded php, lost php file association"
- In reply to: Nico: "Re: Arrays : returning a reference to an array item ??"
- Next in thread: Nico: "Re: Arrays : returning a reference to an array item ??"
- Reply: Nico: "Re: Arrays : returning a reference to an array item ??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 12 Aug 2004 01:32:19 +0100
.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
- Next message: steve: "UTF-8 not decoding"
- Previous message: Kristopher K. Kruger: "Re: Upgraded php, lost php file association"
- In reply to: Nico: "Re: Arrays : returning a reference to an array item ??"
- Next in thread: Nico: "Re: Arrays : returning a reference to an array item ??"
- Reply: Nico: "Re: Arrays : returning a reference to an array item ??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|