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 Link to comment https://forums.phpfreaks.com/topic/215877-add-text-in-array/ 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" ) Link to comment https://forums.phpfreaks.com/topic/215877-add-text-in-array/#findComment-1122193 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 Link to comment https://forums.phpfreaks.com/topic/215877-add-text-in-array/#findComment-1122196 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? Link to comment https://forums.phpfreaks.com/topic/215877-add-text-in-array/#findComment-1122199 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; } Link to comment https://forums.phpfreaks.com/topic/215877-add-text-in-array/#findComment-1122203 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 Link to comment https://forums.phpfreaks.com/topic/215877-add-text-in-array/#findComment-1122212 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). Link to comment https://forums.phpfreaks.com/topic/215877-add-text-in-array/#findComment-1122216 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 Link to comment https://forums.phpfreaks.com/topic/215877-add-text-in-array/#findComment-1122222 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.