Pages

Wednesday, June 1, 2016

Remove Junk characters from php array before convert array in JSON

When you creating APIs then the array contains Junk characters and this array will not convert into JSON so if you want to convert this into JSON then you've to remove the Junk characters first. You can use the following function :

function clear_junk_data($row = array()){
$array = array();
foreach($row as $key => $value){
if(is_array($value) || is_object($value)){
$array[$key] = clear_junk_data($value);
}else{
$array[$key] = preg_replace('/[^a-zA-Z0-9_ %:@\[\]\.\(\)%&<\/!#\$\^+>*=,-]/s', '', $value);
}
}
return $array;

}

How to use :

 $arr = clear_junk_data($arr);

No comments:

Post a Comment