Forum Moderators: coopster
I have a some code which is used to extract links from an array and put them in a new array.
I would like to put this code in a function but I having great difficulty in getting the function to return anything.
My code is:
//- COLLECT ABSOLUTE NON EXTERNAL URLS
foreach( $unsortedLinks as $key=>$value ) {
if (eregi('('.$pageTypes.')',$value)){
if (eregi("^$baseUrl",$value)){
$cleanedLinks[]=$value;
unset($unsortedLinks[$key]);
}
}
}
Simply stuffing this code into an array and calling it doesn't seem to work at all.
Am I missing something, or are there special ways of handling arrays in functions?
Cheers
Chris
Actually my problem is a little more complicated than explained above.
I have a series of codes each to carry out a seperate task eg.
//- COLLECT ABSOLUTE URLS
foreach( $unsortedLinks as $key=>$value ) {
if (eregi('('.$pageTypes.')',$value)){
if (eregi("^$baseUrl",$value)){
$cleanedLinks[]=$value;
unset($unsortedLinks[$key]);
}
}
}
//- REMOVE EXTERNAL URLS
foreach( $unsortedLinks as $key=>$value ) {
if (eregi('^http:',$value)){
$externalLinks[]=$value;
unset($unsortedLinks[$key]);
}
}
The method you mentioned above works great, except when I need to use the unsortedLinks array in different functions and get several different arrays returned. What is the best way of doing this - is it possible to to combine the code above in to 1 function or would I have to create seperate functions for each code. Also I have been reading and understand the concept of making variables 'global' but I am not sure how to apply this to arrays.
Many thanks
Chris
PS. Jatar I may have to start paying you as you have provided me with so much help and answers I feel guilty!
have provided me with so much help and answers I feel guilty!
rofl, isn't that what we're here for? You can pay me back by using what you learn to help other people, that whole viscious circle of kindness thing. ;)
This page talks about Variable Scope in PHP [ca.php.net] and may help with the global question. If you haven't read it see if that explains what you need to know, in essence making an array global works the same way as any other var.
I think I would keep the functions seperate, they have individual functions and that way if you only needed to use one specific piece somewhere else you won't have to wander through the whole thing and make the server do a lot of work for nothing.