EP Posted November 12, 2008 Share Posted November 12, 2008 Ok, I have an array, it's actually the results from a mysql database query. Here's an example: Array ( [0] => Song 2 [title] => Song 2 [1] => Blur [artist] => Blur [2] => 2008-11-12 03:59:51 [timeplayed] => 2008-11-12 03:59:51 ) Array ( [0] => Pale Shelter [title] => Pale Shelter [1] => Tears For Fears [artist] => Tears For Fears [2] => 2008-11-12 03:55:15 [timeplayed] => 2008-11-12 03:40:15 ) Array ( [0] => My Own Prison (Radio Edit) [title] => My Own Prison [1] => Creed [artist] => Creed [2] => 2008-11-12 03:52:01 [timeplayed] => 2008-11-12 03:30:00 ) The results list is much bigger than that but you get the idea. I'm trying to use array_map() to go through each result and modify the "title" value only. I'm trying to get it to remove the song version from the title string like where it says "(Radio Edit)" so the third entry's title value above would only output "My Own Prison" when I go to list the array. I have the code for modifying the string when it is in a while($row = mysql_fetch_array($result)) loop but I just can't get array_map() to modify only that part of the array and print the result. Can someone maybe give me some example code on how to do this? EDIT: Sorry, I forgot to post the code I have so far. Here: $result = mysql_query("SELECT title, artist, timeplayed FROM `songhistory` ORDER BY title"); function stripversion($title) { $title = $row['title']; $condition1 = strripos($title, 'edit'); $pos = strrpos($title, '('); if($pos > 0) { if($condition1 > 0 ){ $title = substr($title, 0, $pos); } return $title; } $titlesfixed = array_map("stripversion", $result); print_r($titlesfixed); Quote Link to comment Share on other sites More sharing options...
redarrow Posted November 12, 2008 Share Posted November 12, 2008 why not use str_replace(' ', ' ' , $varable_name); Quote Link to comment Share on other sites More sharing options...
EP Posted November 12, 2008 Author Share Posted November 12, 2008 Maybe I didn't explain this clear enough. I have a list of results from a mysql query. I need to know how to modify the "title" field in each result of the array. Quote Link to comment Share on other sites More sharing options...
redarrow Posted November 12, 2008 Share Posted November 12, 2008 one example........... <?php $find="b"; $replace_with="redarrow"; $a=array("a","b","c"); if(in_array($find,$a)){ $x=str_replace($find,$replace_with,$a); print_r($x); } ?> Quote Link to comment Share on other sites More sharing options...
laffin Posted November 12, 2008 Share Posted November 12, 2008 preg_replace is better suited for this kinda thing <?php $songs=array( array('title'=>'Song 2','artist' => 'Blur'), array('title'=>'Peter Shelter','artist' => 'Tears for fears'), array('title'=>'My Own Prison (Radio Edit)','artist' => 'Creed') ); foreach ($songs as $key=>$song) { $songs[$key]=preg_replace('/(.*)(\(.*\))/','\1',$song); } echo "<pre>\n"; print_r($songs); echo "</pre>\n"; Quote Link to comment Share on other sites More sharing options...
EP Posted November 12, 2008 Author Share Posted November 12, 2008 Ok got it. Thanks guys! 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.