passing array by reference value is not changing
Moderator: General Moderators
passing array by reference value is not changing
I am passing array by reference to a function & then adding more array to that array, but when function return back i did not get any change to original array.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]Code: Select all
function AssignChilds($child, &$arData, $id)
{
foreach($arData as &$v)
{
if(is_array($v) && $v['id']==$id){
$v['childs'][] = $child;
break;
}
}
}
// some code here
$data= array('id'=>2, 'pid'=>1, 'name'=>'GrandFather', 'childs'=>array(
array('id'=>4, 'pid'=>2, 'name'=>'Father')
)
);
$child = array('id'=>3, 'pid'=>2, 'name'=>'Uncle1');
AssignChilds($child, &$data, 2);
print_r($data);feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
please try
Code: Select all
error_reporting(E_ALL); ini_set('display_errors', true);
function AssignChilds($child, &$arData, $id)
{
foreach($arData as &$v)
{
echo 'v: ', print_r($v); echo "\n";
if(is_array($v) && $v['id']==$id){
$v['childs'][] = $child;
break;
}
}
}