0
$another_array = array();

$array = array(5 => 1, 12 => 2);
make_another_array($array);
print_r($another_array);

function make_another_array(array $array) {

    foreach ($array as $key=>$value) {
       $another_array[$key] = $value;

    }
}

I'm adding values to $another_array inside a function, but when I print the$another_array after calling the function, its giving a blank array.

Why is it so ? How to correctly assign values to $another_array and print outside the function here so that its not blank ?

2
  • 2
    You're not returning the result of the function. The array you assign inside the function has nothing to do with the array you declared outside the function. Hence, to get anything from the function, you'll have to return whatever you want from that function, either with return, echo, print_r or similar. Also, there is no reason to create the variable outside the function, it won't be available inside the function, unless you pass it to the function via an option on calling the function, or make it global within the funciton. Commented Nov 8, 2016 at 1:15
  • Were any of the 4 given answers useful? if so, upvote. If it resolved, mark it. Don't leave a question hanging. Commented May 29, 2017 at 0:49

4 Answers 4

2

You're not capturing or returning the data:

function make_another_array(array $array) {
  foreach ($array as $key=>$value) {
    $another_array[$key] = $value;
  }

  return $another_array; // return here
}

$array = array(5 => 1, 12 => 2);
$another_array = make_another_array($array); // capture the returned value
print_r($another_array);

Secondly, you might just want to use this instead:

$another_array = array_values($array);

As by comment below, there is the pass by reference method:

function update_array(array &$array) {
  $array[3] = 12;
}
update_array($array);
print_r($array);

And finally, the not preferred method..

function update_array(array $a) {
  global $another_array;

  foreach($a as $key => $value){
    $another_array[$key] => $value;
  }
}

// or just array_merge
// $array = array_merge($array, $another_array);
Sign up to request clarification or add additional context in comments.

4 Comments

How can I make $another_array as global so that I can use it without returning ?
I would see that as "bad practice" as it quickly pollutes the scope. There are other methods available.
ok. I was asking in case I need to return more than one value from the function, I should be aware of a different way which I got from danchik's answer below. Just for knowledge. Thank you.
If you need to return more then one value you should consider learning about classes \ objects.
1

You have to return the another_array inside the function: The variables declared inside a function are local-only, and the variables declared outside aren't accessible.

$array = array(5 => 1, 12 => 2);
$another_array = make_another_array($array);
print_r($another_array);

function make_another_array(array $array) {

    $another_array = array();
    foreach ($array as $key=>$value) {
       $another_array[$key] = $value;

    }
    return $another_array;
}

alternatively, you could also pass another array by reference:

$another_array = array();

$array = array(5 => 1, 12 => 2);
make_another_array($array, $another_array);
print_r($another_array);

function make_another_array(array $array, &$another_array) {

    foreach ($array as $key=>$value) {
       $another_array[$key] = $value;

    }
}

or, you could set them inside the function using the global keyword (but I'd recommend using the other two solutions):

$another_array = array();
$array = array(5 => 1, 12 => 2);
make_another_array($array);
print_r($another_array);

function make_another_array(array $array) {

    global $another_array;
    foreach ($array as $key=>$value) {
       $another_array[$key] = $value;

    }
}

and finally you could use the superglobal $GLOBALS, which is accessible in all scopes. (which is pretty the same as the sollution above)

$another_array = array();
$array = array(5 => 1, 12 => 2);
make_another_array($array);
print_r($another_array);

function make_another_array(array $array) {

    foreach ($array as $key=>$value) {
        $GLOBALS['another_array'][$key] = $value;

    }
}

Comments

0

You can pass it by reference like the following:

<?php
$another_array = array();
$array = array(5 => 1, 12 => 2);
function make_another_array($array, &$another_array) {

    foreach ($array as $key=>$value) {
       $another_array[$key] = $value;

    }
    return $another_array;
}
make_another_array($array, $another_array);
print_r($another_array);

http://ideone.com/mAAeTp

Comments

0

make sure there is a reference to $another_array as global to point to the one outside of function, otherwise it is local only.

global $another_array; // put this at the beginning of the make_another_array( function.

$another_array = array();
$array = array(5 => 1, 12 => 2); 
make_another_array($array); 
print_r($another_array); 

function make_another_array(array $array) 
{ 
    global $another_array;

    foreach ($array as $key=>$value){

        $another_array[$key] = $value; 

    }


}

9 Comments

Why was this voted down?? Accepted answer DID NOT answer the question but provided a different solution. Question was HOW TO properly assign to global variable from with in the function. and you do it by declareing the global variable inside the function as global $another_array;
up voted. How to solve in this method ? Can you post the code here ?
$another_array = array(); $array = array(5 => 1, 12 => 2); make_another_array($array); print_r($another_array); function make_another_array(array $array) { global $another_array; // <--- this line foreach ($array as $key=>$value) { $another_array[$key] = $value; } }
just add the as the first line inside the function global $another_array)
just add the as the first line inside the function global $another_array; sorry with all the typos
|

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.