PHP Echo数组外部函数

I'm curious to know how to echo an array outside a function, for instance, this code prints it within. but what if I just wanted to return the values and echo it outside?

function myArray() {
    $arr = array('One', 'Two', 'Three', 'four');

    for ($i = 0; $i < count($arr); $i++) {
        echo $arr[$i];
    }
}

myArray();

Hope that makes sense.

So, it's not that you would like to echo() outside the loop, it's that you would like the values within your array to be accessible.

Using the array() function (which is actually a language construct) and offering only values, the array will be "keyed" for you by PHP. These keys begin at 0 and can be accessed with square brackets, like so:

$myArray = array("first", "second", "third");
echo $myArray[0]; // will echo "first"
echo $myArray[1]; // will echo "second"

If you wish to build the array within a function, then you need to be aware of scope. Variables set within functions are not accessible outside of functions. In order to achieve this, you need to return the array and capture the return of that function call:

function myArray() {
  return array("first", "second", "third");
}

// capture the return value of the myArray() function
$arrayToEcho = myArray();

echo $arrayToEcho[0]; // will echo "first"

just return array created

<?php
  function myArray()
  {
      return array('One', 'Two', 'Three', 'four');
  }

  $arr = myArray();
  echo $arr[1];
?>

Try this:

function myArray() {

    $arr = array('One', 'Two', 'Three', 'four');
    $output = "";
    for($i=0; $i < count($arr); $i++) {
        $output .= $arr[$i] . "<br />";
    }
    return $output;
}

$theValuesIWant = myArray();
echo $theValuesIWant ;

I'm not sure exactly what you are asking, I think you mean as $arr is declared in myArray it is a local variable, but you want to use it out side of the myArray function, to do this you would return it like so:

function myArray() {
    return array('One', 'Two', 'Three', 'four');
}

$array = myArray();

for ($i = 0; $i < count($array); $i++) {
    echo $array[$i];
}

or

print_r(myArray());

I'm not entirely sure what you are asking for. You can use print_r or var_dump to print an array, but this is usually only used for debugging purposes. you can also create a function like this that would allow you to pass an array in as a parameter to print.

function printArray($printArr) {
    $output = "";
    for($i=0; $i < count($printArr); $i++) {
        $output .= $printArr[$i];
    }
    echo $output;
}

$arr = array('One', 'Two', 'Three', 'four');
printArray($arr);

Because I like array_walk, an alternate solution,

$arr = array('One', 'Two', 'Three', 'four');
array_walk($arr, 'echo_it');

function echo_it($item) {
    echo "$item ";
}

but not as simple as just printing

print_r(array('One', 'Two', 'Three', 'four'));

or an implode

echo implode(", ", array('One', 'Two', 'Three', 'four'));

All of the above will echo the array contents in one form or another. It depends on what sort of output you want, for example comma separated, or as a list…

function myArray() {
    $arr = array('One', 'Two', 'Three', 'four');

    // Here goes whatever you do with $arr

    return $arr;
}

$arr = myArray();
echo $arr[0];

However introducing hardcoded values like that inside functions is not always a good idea which is why you could use an alternative approach and create your original array ($arr) outside of the myArray() function and then pass it by reference to the function.

That way everything you do inside the function to affect $arr will be visible outside the myArray() function.

Passing by reference

$arr = array('One', 'Two', 'Three', 'four');

function myArray(&$arr) { // Note the ampersand sign (&)!
    // Whatever you do with the original array
    $arr[] = 'Five';
}

myArray($arr);
echo $arr[4]; // Will echo "Five"...