Jump to content

[SOLVED] Array_Map Help


EP

Recommended Posts

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);

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.