Remove string “Array” from print_r()'s output

杀马特。学长 韩版系。学妹 提交于 2021-02-08 07:40:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!