Jump to content

changing array value


sax

Recommended Posts

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

Link to comment
Share on other sites

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 by

1. 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 by requinix
Link to comment
Share on other sites

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 by Psycho
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.