问题
How can I remove the string "Array" in the printout from print_r()
?
I have tried using string replace, but it didn't work.
Example output:
Array (
[0] => Array (
[id] => Classify318721363801824
[classification] =>
Array (
[0] => Array (
[Klasifikasi] => NonOpini
[Nilai] => 0.999946 )
[1] => Array (
[Klasifikasi] => Opini
[Nilai] => 5.43418e-005 ) )
回答1:
Set return flag
(secont func arg) at TRUE
for print_r()
function to return array print as string, than replace whatever you want using str_replace.
echo str_replace('Array','',print_r($arr,true));
I suppose you also want to replace unneeded new lines. Here's my own func to print arrays nicely.
function aprint($arr, $return = false) {
$wrap = '<div style=" white-space:pre; position:absolute; top:10px; left:10px; height:200px; width:100px; overflow:auto; z-index:5000;">';
$wrap = '<pre>';
$txt = preg_replace('/(\[.+\])\s+=>\s+Array\s+\(/msiU','$1 => Array (', print_r($arr,true));
if ($return) return $wrap.$txt.'</pre>';
else echo $wrap.$txt.'</pre>';
}
来源:https://stackoverflow.com/questions/15531201/remove-string-array-from-print-rs-output