如何将斜杠添加到多维数组值

时间:2013-02-22 14:09:20

标签: php multidimensional-array

var_dump($array);给出:

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [name] => Mark O'Donel
                    [id] => 35802624
                )

            [1] => Array
                (
                    [name] => Da's Messager
                    [id] => 522505962
                )
        )
)

知道如何添加斜杠来对值进行排列吗? 尝试以下但不起作用:

foreach($array as $list){
    foreach($list as $elt){
       $name = $elt['name'];
       $id = $elt['id'];

       echo 'before: '. $name .'<br><br>'; // Assuming $name= "Mark O'Donel"
       $list[$id] = addslashes($name);
       echo 'after: '. $elt['name'] .'<br><br>'; //suppose to give Mark O\'Donel
    }
}

1 个答案:

答案 0 :(得分:0)

必须将更改后的值分配给数组变量:

foreach($array as $list){
    foreach($list as $index=>$elt){
       $name = $elt['name'];
       $id = $elt['id'];

       echo 'before: '. $name .'<br><br>'; // Assuming $name= "Mark O'Donel"
       $list[$index]['name'] = addslashes($name);
       echo 'after: '. $elt['name'] .'<br><br>'; //suppose to give Mark O\'Donel
    }
}