mraza Posted October 14, 2010 Share Posted October 14, 2010 Hi , I need to add text in an array, my array shows like: Array ( [0] => /filse/file.php [1] => /files/file2.php [2] => /files/file3.php ) so i need to add url in front of them and it will become like this Array ( [0] => http://mysite.com/filse/file.php [1] => http://mysite.com/files/file2.php [2] => http://mysite.com/files/file3.php ) Any idea please. Thanks Quote Link to comment Share on other sites More sharing options...
atrum Posted October 14, 2010 Share Posted October 14, 2010 why not just do this? array ( [0] => "http://mysite.com/filse/file.php" [1] => "http://mysite.com/files/file2.php" [2] => "http://mysite.com/files/file3.php" ) Quote Link to comment Share on other sites More sharing options...
mraza Posted October 14, 2010 Author Share Posted October 14, 2010 bcoz i need to add random urls there in loop Quote Link to comment Share on other sites More sharing options...
atrum Posted October 14, 2010 Share Posted October 14, 2010 Can you describe in more detail about what you are trying to accomplish? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 14, 2010 Share Posted October 14, 2010 $url = 'http://mysite.com'; $array = preg_replace('#(.*)#', $url . '\1', $array); //or foreach($array as &$value) { $value = $url . $value; } Quote Link to comment Share on other sites More sharing options...
mraza Posted October 14, 2010 Author Share Posted October 14, 2010 Thanks AbraCadaver i only needed to add $value[] and it worked perfect Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 14, 2010 Share Posted October 14, 2010 Thanks AbraCadaver i only needed to add $value[] and it worked perfect That creates a new array called $value. What I posted will change the original array (assuming you have PHP 5). Quote Link to comment Share on other sites More sharing options...
mraza Posted October 14, 2010 Author Share Posted October 14, 2010 This exactly what i did $url = 'http://www.mysite.com'; $newarray= array(); foreach($array[0] as &$value) { $newarray[] = $url . $value; } and it worked perfect as i am already in a loop with $array Thanks again Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.