2

I have several arrays that are created in a PHP function. I want to return all of those arrays in the return value when the function returns. However, since I can only return one array, I create an array of arrays, like so:

$return_value = array($a_config_lines, $port_values, $proto_values, $dev_values, $ca_values, $key_values, $crt_values, $key_values, $group_values, $user_values, $dh_values, $server_values, $ifconfig_pool_values, $keepalive_values, $comp_values, $verb_values, $status_values, $management_values, $a_extra_config_settings);
return $return_value;

$return_value is an array, that contains all of the other arrays, like $comp_values, $verb_values, etc.

When I return from the function, I want to immediately REVERSE what I just did. So I want to take $return_value, and split it up into the old individual arrays, but now they keys for those arrays are 0,1,2, etc... is there an easy way to do this?

Or will I have to manually set all of the keys before I return the array?

Thanks!

4

1 回答 1

0

如果您返回的数组是关联的,您可以在返回extract()时将所有组件数组拉回全局范围:

// Define your array as an associative array, keys named the same as the component variables
return = array(
  'a_config_lines' => $a_config_lines, 
  'port_values' => $port_values, 
  'proto_values' => $proto_values, 
  // etc...
);

在调用你的函数时:

// extract() the returned array, to dump all the vars back into global scope
$return = your_function();
extract($return);
// All arrays are now at global scope
var_dump($port_values);
var_dump($a_config_lines);

但是,当您可以从数组中整齐地访问它们时,将它们提取回全局范围几乎没有价值。这使您免于用一堆新变量污染全局范围。

// Better! Just use the array.
$return = your_function();
var_dump($return['port_values']);
于 2012-08-10T02:49:22.393 回答