dtyson2000 Posted November 4, 2006 Share Posted November 4, 2006 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);//returnsone, eight, onetwo, onefour[/code]How can I get this to return "one, eight, twelve, fourteen"???Thank you, in advance for your help! Quote Link to comment https://forums.phpfreaks.com/topic/26115-resolved-str_replace-number-array/ Share on other sites More sharing options...
Zane Posted November 4, 2006 Share Posted November 4, 2006 [code=php:0]foreach($typearr as &$val) { str_replace($typecodes, $typedesc, $val);}print_r($typearr);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/26115-resolved-str_replace-number-array/#findComment-119430 Share on other sites More sharing options...
dtyson2000 Posted November 4, 2006 Author Share Posted November 4, 2006 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);//returnsone, 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. Quote Link to comment https://forums.phpfreaks.com/topic/26115-resolved-str_replace-number-array/#findComment-119437 Share on other sites More sharing options...
alpine Posted November 4, 2006 Share Posted November 4, 2006 [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] Quote Link to comment https://forums.phpfreaks.com/topic/26115-resolved-str_replace-number-array/#findComment-119501 Share on other sites More sharing options...
dtyson2000 Posted November 4, 2006 Author Share Posted November 4, 2006 Excellent, Alpine!Your suggestion works like a charm. Thank you SO much! I was nearly bald with ripping my hair out!This place is great.Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/26115-resolved-str_replace-number-array/#findComment-119574 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.