Question

I have this array :

Array
(
    [0] => Array
        (
            [0] => Windows#XP
            [1] => 3620
        )

    [1] => Array
        (
            [0] => Windows#Vista
            [1] => 1901
        )

    [2] => Array
        (
            [0] => Windows#7
            [1] => 88175
        )

and so on...I want to replace # with an space here's my code and it doesnt seem to work:

$tab_os = str_replace('#',' ',$tab_os);

Any solution for that?

Thanks!

Was it helpful?

Solution

You can try this

foreach ($tab_os as $key => $value){
   $tab_os[$key]  = str_replace('#',' ',$value);
}

but really, str_replace accepts and returns arrays so this shouldn't be the problem. see docs


I have tried and tested this code, try it here: http://codepad.org/Ok1fZ16O

$tab_os = array( array( "Windows#XP", 1 ), array( "Windows#7", 1 ) );

foreach ($tab_os as $key => $value) {
   $tab_os[$key]  = str_replace('#', ' ', $value);
}

var_dump($tab_os);

OTHER TIPS

Because loops are too mainstream (and also because it's a bit faster), you can use array_map() :

function replaceValue($value) {
    return str_replace('#', ' ', $value[0]);
}

array_map('replaceValue', $tab_os);
$tab_os = array(
    array('Windows#XP', 3620),
    array('Windows#Vista', 1901),
    array('Windows#7', 88175),
);

foreach ($tab_os as $os_k => $os_v){
   $tab_os[$os_k] = str_replace('#', ' ', $os_v);
}
print_r($tab_os);

You need to loop through it

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