sax Posted May 8, 2013 Share Posted May 8, 2013 Hy to all, basicly all I want to do is: I have an array of three elements, the firt to elements can be a string or a number for example. vect[0]=12, vect[1]=JULY, or vect[0]=first,vect[1]=2 and so on. all I want to do is to change the array value in base of it value. so if vect[0]=first I want to substitute it with 1, and if vect[1]=JULY I want to substitute it with 7. The only solution I can think of is to make a series of IF's but I don't like it, I think there is a bettere way to do it. this is my script $date = "riunione del 1/aprile/1969 orco zio"; $p="!(primo|due|tre|quattro|cinque|sei|sette|otto|nove|dieci|undici|dodici|tredici|quattordici|quindici|sedici|diciassette|diciotto|diciannove|venti|ventuno|ventidue|ventitre|ventitré|ventitrè|ventiquattro|venticinque|ventisei|ventisette|ventotto|ventinove|trenta|trentuno|PRIMO|DUE|TRE|QUATTRO|CINQUE|SEI|SETTE|OTTO|NOVE|DIECI|UNDICI|DODICI|TREDICI|QUATTORDICI|QUINDICI|SEDICI|DICIASSETTE|DICIOTTO|DICIANNOVE|VENTI|VENTUNO|VENTIDUE|VENTITRE|VENTITRÉ|VENTITRÈ|VENTIQUATTRO|VENTICINQUE|VENTISEI|VENTISETTE|VENTOTTO|VENTINOVE|TRENTA|TRENTUNO|\d(?:\d)?)[-/](GENNAIO|FEBBRAIO|MARZO|APRILE|MAGGIO|GIUGNO|LUGLIO|AGOSTO|SETTEMBRE|OTTOBRE|NOVEMBRE|DICEMBRE|gennaio|febbraio|marzo|aprile|maggio|giugno|luglio|agosto|settembre|ottobre|novembre|dicembre|[0-9])[-/](\d\d(?:\d\d)?)*!"; if (preg_match($p,$date,$matches)) { echo $matches[1]; echo $matches[2]; echo $matches[3]; } some has some ideas? thank's Quote Link to comment https://forums.phpfreaks.com/topic/277804-changing-array-value/ Share on other sites More sharing options...
requinix Posted May 8, 2013 Share Posted May 8, 2013 (edited) PHPFreaks.com Questions, Comments, & Suggestions This is NOT a help forum! Do not post topics asking for help that are not related to the website. Tip: strtr can do a bunch of replacements using an associative array, like array( "first" => "1", "JULY" => "7",[edit] And if you're always converting to a number then you can save yourself some repetition by1. Converting the string to all upper- or lowercase (don't need a "july" and "JULY") 2. Using iconv() to "remove" accents from characters (don't need a "ventitre" and "ventitré") Edited May 8, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/277804-changing-array-value/#findComment-1429137 Share on other sites More sharing options...
Psycho Posted May 8, 2013 Share Posted May 8, 2013 (edited) One possible solution: //Define all the replacements (in lower case) $replacements = array( 'first' => 1, 'second' => 2, 'third' => 3, 'july' => 7, 'august' => 8 ); function convertValues($value) { global $replacements; $testValue = trim(strtolower(iconv($value))); if(array_key_exists($testValue, $replacements)) { return $replacements[$testValue]; } return $value; } //Usage $vect = array_map('convertValues', $vect); Note that I used "global" to avoid redefining the array in the function. Since the function is called as a 'callback' from within the array_map() function you can't pass additional parameters. Well, you can, but it's a little ambiguous and I'm too lazy to look up how it's done. So, one solution would be to make this a class where the replacement values are a property in the class. Edited May 8, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/277804-changing-array-value/#findComment-1429144 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.