macwise Posted August 30, 2010 Share Posted August 30, 2010 Though I've been tinkering with PHP for a number of years, I'm mostly a front end developer/designer (HTML5, XHTML, CSS). In other words, please go easy on me. I am working on a site where we're pulling data from mysql. Each row that has a column for dimensions which look like this: 20x10x5 Here's what I have so far: $dimensions = explode("x",$row['DIMENSIONS']); $x_separated = implode("x", $dimensions); echo "$x_separated"; What I would like to do is append an "inches" or "in" to each value in the array. The resulting values should look like this: [0] => 20in [1] => 10in [2] => 5in And after the implode, would then look like this: 20in x 10in x 5in Any help from the more experienced coders here is much appreciated. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/212116-append-every-value-in-an-array/ Share on other sites More sharing options...
ldb358 Posted August 30, 2010 Share Posted August 30, 2010 foreach($dimensions as $ key => $value){ $dimensions[$key] = $value ."in" } in between your to statements Quote Link to comment https://forums.phpfreaks.com/topic/212116-append-every-value-in-an-array/#findComment-1105381 Share on other sites More sharing options...
macwise Posted August 30, 2010 Author Share Posted August 30, 2010 Thanks, ldb358! That worked perfectly! Here is the resulting code, for anyone with the same problem: $dimensions = explode("x",$row['DIMENSIONS']); foreach($dimensions as $key => $value){ $dimensions[$key] = $value ."in"; } $x_separated = implode(" x ", $dimensions); echo "$x_separated"; Quote Link to comment https://forums.phpfreaks.com/topic/212116-append-every-value-in-an-array/#findComment-1105386 Share on other sites More sharing options...
samshel Posted August 30, 2010 Share Posted August 30, 2010 just another way of doing it: echo $x_separated = ereg_replace("x", "in x", $row['DIMENSIONS']); Quote Link to comment https://forums.phpfreaks.com/topic/212116-append-every-value-in-an-array/#findComment-1105392 Share on other sites More sharing options...
fortnox007 Posted August 30, 2010 Share Posted August 30, 2010 Thanks, ldb358! That worked perfectly! Here is the resulting code, for anyone with the same problem: $dimensions = explode("x",$row['DIMENSIONS']); foreach($dimensions as $key => $value){ $dimensions[$key] = $value ."in"; } $x_separated = implode(" x ", $dimensions); echo "$x_separated"; Thx for sharing ; ) Quote Link to comment https://forums.phpfreaks.com/topic/212116-append-every-value-in-an-array/#findComment-1105403 Share on other sites More sharing options...
jcbones Posted August 31, 2010 Share Posted August 31, 2010 Ereg is depreciated. Of course you could do: echo str_replace('x','in. x ',$row['DIMENSIONS']) . 'in.'; Quote Link to comment https://forums.phpfreaks.com/topic/212116-append-every-value-in-an-array/#findComment-1105413 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.