Jump to content

[RESOLVED] str_replace number array


dtyson2000

Recommended Posts

It's me again...

I'm having a problem with str_replace and an array with numbers. Basically I have the following happening:

[code]
$typearr = "1, 8, 12, 14";

//snip

$typecodes = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12");

$typedesc = array("one, ", "two, ", "three, ", "four, ", "five, ", "six, ", "seven, ", "eight, ", "nine, ", "ten, ", "eleven, ", "twelve");

$type = str_replace($typecodes, $typedesc, $type);

//returns

one, eight, onetwo, onefour
[/code]

How can I get this to return "one, eight, twelve, fourteen"???

Thank you, in advance for your help!
Link to comment
Share on other sites

I truly apologize but I forgot to mention that the typearr is an exploded array;

[code]
if ($type)
{
$typearr = explode(', ', $_POST['type_array']);
};

//array may be set to $typearr = "1, 8, 12, 14";

//snip

$typecodes = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12");

$typedesc = array("one, ", "two, ", "three, ", "four, ", "five, ", "six, ", "seven, ", "eight, ", "nine, ", "ten, ", "eleven, ", "twelve");

$type = str_replace($typecodes, $typedesc, $type);

//returns

one, eight, onetwo, onefour
[/code]

does that affect your solution? It didn't seem to do the trick, as it returns:

[code]
Array ( [0] => )
[/code]

Again, I apologize.
Link to comment
Share on other sites

[code]

<?php

$new = array();
$typearr = array(1, 8, 12, 14, 33); // or exploded post or whatever
$dignames = array("null","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen");

foreach($typearr as $number)
{
  $number = trim($number);
  if(array_key_exists($number, $dignames)) $new[] = $dignames[$number]; else $new[] = $number;
}

echo implode(", ",$new);

?>

[/code]
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.