2

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 ) )
7
  • Maybe you’re looking for something like var_dump or var_export? Commented Mar 20, 2013 at 17:57
  • 1
    Can you explain why you WANT to do this? Commented Mar 20, 2013 at 17:58
  • 2
    Why didn't string replace work? What was your code for that? Did you not pass print_r() TRUE as the second argument, thereby causing it to output rather than return a string? Commented Mar 20, 2013 at 17:59
  • Are you trying to use print_r() for actual user facing output? Its purpose is debugging. Commented Mar 20, 2013 at 17:59
  • 1
    @user2192076, it depends of what you want to get at the end. If just to remove some useless text - then use print_r() with return flag and replace whatever u want. If you need something more complex to do with array elements, then you should write a recursive function to walk through your array and handle with it. Commented Mar 20, 2013 at 18:16

1 Answer 1

2

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>';
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is probably what the asker was trying to do. Unfortunately it's an indiscriminate replacement, so any array contents that contain "Array" will also be replaced. That's probably undesirable.
@Wiseguy, thanks for your notice. I've updated my func code to search for an Array string more strictly. It still can be tricked, by I think it isn't really serious issue. Anyway it can be upgraded even further if someone need it.
@DenisO. Your preg_replace line is not working for me. It has no effect on the formatting of an associative array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.