John Davidson

php - Is str_replace done when there is an Array in the Array?

0 comments
Message:


$blocked_list   = array(';', '/*','---','*','\*','\'','select','union','join','where','insert','like','drop','and','have','alter','0x','"','\'','location','LOCATİON','LOCATION','refresh','script','frame','iframe','İFRAME','IFRAME','\\n');

$check_array = [
[
'key' => 'select',
'lang' => [
'en' => 'Select',
'tr' => 'Sec'
]
]
];
$new_tk = str_ireplace($blocked_list, '', $check_array);

It gives me the following error as a result.


"Warning: Array to string conversion in /var/www/...."
How can I str_replace the array inside the array as in the $array_check variable?



My Answer:
You cannot directly use str_replace on a multidimensional array like $check_array. Instead, you can loop through the array and apply str_replace on each element individually. Here is an example code snippet to achieve this:

php
$blocked_list = array(';', '/*','---','*','\*','\'','select','union','join','where','insert','like','drop','and','have','alter','0x','"','\'','location','LOCATİON','LOCATION','refresh','script','frame','iframe','İFRAME','IFRAME','\\n');

$check_array = [
[
'key' => 'select',
'lang' => [
'en' => 'Select',
'tr' => 'Sec'
]
]
];

foreach ($check_array as $key => $value) {
foreach ($value as $sub_key => $sub_value) {
if (is_array($sub_value)) {
foreach ($sub_value as $lang_key => $lang_value) {
$check_array[$key][$sub_key][$lang_key] = str_ireplace($blocked_list, '', $lang_value);
}
} else {
$check_array[$key][$sub_key] = str_ireplace($blocked_list, '', $sub_value);
}
}
}

print_r($check_array);


This code will loop through the $check_array and apply str_replace on each element, including the elements inside the nested arrays.

Rate this post

3 of 5 based on 1960 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us