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
https://forums.phpfreaks.com/topic/26115-resolved-str_replace-number-array/
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.
[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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.