Page 1 of 1
passing array by reference value is not changing
Posted: Fri Jul 27, 2007 1:25 am
by ali_engr
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.
Posted: Fri Jul 27, 2007 1:53 am
by John Cartwright
We can't help without seeing code.
Posted: Fri Jul 27, 2007 2:04 am
by ali_engr
feyd | Please use 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
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]
Posted: Fri Jul 27, 2007 2:05 am
by John Cartwright
Please put your code in [syntax=php][/syntax] tags
Posted: Fri Jul 27, 2007 6:25 am
by volka
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;
}
}
}