Question

hi everyone i have problem. i have the code like this

$foo = array(':1:',':2:',':3:4:');

and I want the result is like this

$foo = array('1','2','3,4');

thank's before for help :) sorry for my english

Was it helpful?

Solution

array_map() with a callback using trim() and str_replace() will do the trick:

$foo = array_map(function($val) {
    $val = trim($val, ':');
    return str_replace(':', ',', $val);
}, $foo);

OTHER TIPS

Use the array_map() function to traverse and modify an array:

$foo = array_map(function($v) {
    return str_replace(':', ',', trim($v, ':'));
}, $foo);

I'm assuming you want to trim off leading and trailing colons and convert the others to commas. Your question isn't very clear in describing requirements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top