2013-02-22 50 views
1

var_dump($array);給出:如何addslahes到多維數組值

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 
    } 
} 
+5

爲什麼你要和addslashes? – 2013-02-22 14:10:40

+0

這個想法是有一個json編碼值。撇號的跡象似乎與創建問題json解析的值jquery – Lomse 2013-02-22 14:14:32

+4

'json_encode'負責所有您需要的轉義... – knittl 2013-02-22 14:15:20

回答

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 
    } 
} 
+1

你能解釋一下你有什麼改變,爲什麼? – Ikke 2013-02-22 14:29:24

+0

添加斜槓後的值在數組變量中未被更新。 – 2013-02-23 13:56:30