[英]PHP replace characters in array
我正在嘗試使用strtr()
function 從 mysql 結果數組中替換引號和其他字符。 但我收到Warning: strtr() expects parameter 1 to be string, array given
和 null 值。
這是我的代碼:
while($rows = mysqli_fetch_assoc($list)){
$string_filter = array(';'=>'','['=>'',']'=>'','{'=>'','}'=>'','"'=>'',"'"=>'');
$data[$rows['id']] = strtr($rows,$string_filter);
}
$data
是一個數組,我想刪除/修剪$string_filter
數組中指定的字符。 下面是$data
的內容示例:
Array ( [1] => Array ( [id] => 1 [user_id] => 204554 [name] => Rich [group] => PL [ai] => BA [image] => 204554-35849.jpg ) [2] => Array ( [id] => 2 [user_id] => 124534 [name] => James [group] => SS [ai] => IT [image] => 123141-12312.jpg )...
快速提問,您確定這些字符確實存在於您的數據中嗎? 因為您嘗試刪除的字符是 php 將自動 append 到一個數組,當通過 print_r function 查看該數組時。 准確地說,這個說法
print_r(array('key' => 'value'));
將 output
Array([key] => value)
即使很難,arrays 數據本身也沒有實際的[
或]
。
在任何情況下,此代碼段都將清除數組的鍵和值:
$string_filter = array(';'=>'','['=>'',']'=>'','{'=>'','}'=>'','"'=>'',"'"=>'');
while($rows = mysqli_fetch_assoc($list)){
$cleanedrow = [];
foreach($rows as $key => $value) {
$cleanedrow[strtr($key, $string_filter)] = strtr($value, $string_filter);
}
$data[$rows['id']] = $cleanedrow;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.