Search code examples
phphtmlarrays

How to present print_r()'s array output on one line (with no newlines)


How would I print the following in one line?

$product_array = array(
    "id" => 001,
    "description" => "phones",
    "type" => "iphone"
);

print "Print the product_array = ";
print_r($product_array);

Current result

Print the product_array =Array
(
[id] => 001 
[description] => phones 
[type] => iphone 
)

Wanted Result

Print the product_array =Array ( [id] => 001 [description] => phones [type] => iphone )

Solution

  • $arrayString = print_r($array, true);
    echo str_replace("\n", "", $arrayString);
    

    The second value of print_r lets the function return the value instead of printing it out directly.