Cinner Posted May 2, 2010 Share Posted May 2, 2010 Hi guys, I have a multidimensional array containing movie data. One of the elements is a movie title, and for each title that starts with "The", I want to cut that part and put it at the end of the element value (like "Terminator, The"). This would be easy if I would do this during a regular foreach loop echoing all the movie titles, but what I want to do is update the array itself, since I will be doing other things with this array later. Here's an example of what my array could look like: $movies = Array ( [0] => Array ( [id] => 1 [year] => 1984 [title] => The Terminator), [1] => Array ( [id] => 2 [year] => 1994 [title] => True Lies) ); The real array is a bit more complex than that but you get the picture. How do I go through it and look for "The" in the title, and put it at the end of the movie title? Link to comment https://forums.phpfreaks.com/topic/200451-modify-multidimensional-array-values-that-meet-a-criterion/ Share on other sites More sharing options...
Alex Posted May 2, 2010 Share Posted May 2, 2010 $movies = Array( 0 => Array( 'id' => 1, 'year' => 1984, 'title' => 'The Terminator' ), 1 => Array( 'id' => 2, 'year' => 1994, 'title' => 'True Lies' ) ); $movies = array_map( create_function( '$movie', 'if(strtolower(substr($movie["title"], 0, 3)) == "the") { $movie["title"] = substr($movie["title"], 4) . ", " . substr($movie["title"], 0, 3); } return $movie;' ), $movies ); print_r($movies); Output: Array ( [0] => Array ( [id] => 1 [year] => 1984 [title] => Terminator, The ) [1] => Array ( [id] => 2 [year] => 1994 [title] => True Lies ) ) Link to comment https://forums.phpfreaks.com/topic/200451-modify-multidimensional-array-values-that-meet-a-criterion/#findComment-1051959 Share on other sites More sharing options...
Cinner Posted May 3, 2010 Author Share Posted May 3, 2010 Excellent, that was most helpful. Thanks! Link to comment https://forums.phpfreaks.com/topic/200451-modify-multidimensional-array-values-that-meet-a-criterion/#findComment-1052281 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.