[PHP] scope and return array( ); problems
hi all, I have this function: function preg_array($pattern, $array, $r_array) { global $match; global $r_array; foreach ($array as $key = $value) { if (preg_match($pattern, $value,$match)) { $GA=array_slice($match,1); break; } } return array ($GA, $match); }
[PHP] scope and return array( ); problems
Hi all, I have this code ( but you already know it ;-) : function preg_array($pattern, $array, $r_array) { global $match; global $r_array; foreach ($array as $key = $value) { if (preg_match($pattern, $value,$match)) { $r_array=array_slice($match,1); break; } } return array ($r_array, $match); } $GA = array(); preg_array(/^GA\s+(\d+.\d+)\s+(\d+.\d+)/i, $PF, $GA) but the array $GA is empty outside the function, i tried to declare $GA also as global inside the function and it didn't change the output. It works if the function is change to this : function preg_array($pattern, $array, $GA) { global $match; global $GA; foreach ($array as $key = $value) { if (preg_match($pattern, $value,$match)) { $GA=array_slice($match,1); break; } } return array ($GA, $match); } What's wrong with the first version of the preg_array function? do I really need to have the same array name in order to retrieve the third argument of the function? Thanks for your help, yvan
Re: [PHP] scope and return array( ); problems
On Wed, April 18, 2007 5:33 pm, Yvan Strahm wrote: I have this function: function preg_array($pattern, $array, $r_array) { global $match; global $r_array; Passing in $r_array *AND* declaring it global is just plain silly. Do one or the other, but not both. If the array is LARGE, you may also want to consider writing this with array_map and a create_function so that you can have the iteration happening in C instead of in PHP... -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php