passing array by reference value is not changing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ali_engr
Forum Newbie
Posts: 12
Joined: Thu Jun 21, 2007 8:08 am

passing array by reference value is not changing

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

We can't help without seeing code.
ali_engr
Forum Newbie
Posts: 12
Joined: Thu Jun 21, 2007 8:08 am

Post by ali_engr »

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]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Please put your code in [syntax=php][/syntax] tags
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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;
    }
  }
}
Post Reply