0

My problem is as follows. I have a multidimensional array. I declare my array. Then, I run some code that populates my array partially. Then i run a function, which among others is supposed to modify some item in my array from within the function. This is unfortunately not working. So my question is simple. Is it normal? And if yes, how can I overcome this. Thank you very much in advance for your replies. Cheers. Marc.

$list = array([0]=>
                   array(
                         [name]=>'James' 
                         [group]=>''
                         )
             ); 



my_function();
print_r($list); 

function my_function(){
     //some code here
     $list[0]['group'] = 'groupA';
}
1
  • I take it your array is taken from print_r, and not actually [0] =>? Commented Apr 14, 2012 at 17:21

4 Answers 4

2

you could pass the array to the function by reference

my_function(&$list) {

    $list[0]['group'] = 'groupA';
}

$list = /*...*/
my_function($list);

or simply return the array from the function

my_function($list) {

    $list[0]['group'] = 'groupA';
    return $list;
}

$list = /*...*/
$list = my_function($list);

or use a global

my_function() {

    global $list;
    $list[0]['group'] = 'groupA';
}

$list = /*...*/
my_function();
Sign up to request clarification or add additional context in comments.

Comments

1

In your case, the array is not changed, because it is in the global scope, and unlike other languages, PHP do not provide automatic access to the global scope from within function. So you have to do:

function my_function(){
     global $list;
     //some code here
     $list[0]['group'] = 'groupA';
}

But even better, pass the array as an argument by reference to the function

function my_function(&$list){
     //some code here
     $list[0]['group'] = 'groupA';
}

2 Comments

Helo Darhazer. Tks a lot. Both soution working great. Why is the second one better?
Because it will work even if you rename the variable in the global space, or in case you need to perform the same operation to different arrays. Generally, avoid using global variables (because you can accidently override a global variable), and coupling (with global you are working with the exactly one array, and passing it by reference enable you to work with any array)
1

Something like this:

$list = array( 0 =>
                   array(
                         'name'=>'James' 
                         'group'=>''
                         )
             ); 



my_function($list);
print_r($list); 

function my_function(&$list){
     //some code here
     $list[0]['group'] = 'groupA';
}

alternatively:

$list = array(0 =>
                   array(
                         'name'=>'James' 
                         'group'=>''
                         )
             ); 



$list = my_function($list);
print_r($list); 

function my_function($list){
     //some code here
     $list[0]['group'] = 'groupA';
     return $list;
}

1 Comment

Hello Joe. Thanks for trying to help. Unfortunately, I tried that but it is not working...
0

add ' in your internal array indexes

 array(
                         'name'=>'James' 
                         'group'=>''
      )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.