All TalkersCode Topics

Follow TalkersCode On Social Media

Convert Array To String In PHP

Last Updated : Apr 9, 2023

Convert Array To String In PHP

In this article we will show you the solution of convert array to string in PHP, to convert an array into a string, we used two different functions here – implode() and array_reduce().

  • implode(): the implode() function in php is used to join array items in a string and return the joined string.
  • Array_reduce(): the array_reduce() function is used to reduce the array elements into a single string.

Step By Step Guide On Convert Array To String In PHP :-

In the example below we used two different functions to convert an array to a string.

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> convert array to string in php </title>
    <style>
        body {
            font-family : 'Lucida Sans', Verdana , sans-serif ;
        }
        h1 {
         font-size : larger ;
         font-weight : bolder ;
         color : rgb(113, 221, 113) ;
         text-align : center ;
        }
        h3 {
            text-align : center ;
        }
    </style>
</head>
<body>
    <h1> TALKERSCODE </h1>
    <h3> convert array to string in php </h3>
</body>
</html>
<?php
$fruits = array ("apple", "orange", "strawberry", "banana", "peach", "avocado", "kiwi") ;
$result = implode (",", $fruits) ;
print_r ($result) ;
echo "<hr />" ;
function reduction ($carry, $item) {
    $carry .= "$item, " ;
    return $carry ;
}
$str = array_reduce ($fruits, "reduction") ;
print_r ($str) ;
?>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. <style> tag to add CSS to the HTML page
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. <h1> tag used to add heading here.
  8. Close the HTML tag with </html>
  9. <?php tag to write the php.
  10. Creating an array named $fruits first
  11. implode() to convert it into string.
  12. Using echo to display the string
  13. Creating e function named reduction to return every item of the array
  14. Using array_reduce() to convert the array elements into a string
  15. Using echo to display the string
  16. Close the php code with ?> tag

Conclusion :-

Last, here in conclusion, here we can say that with this article’s help, we know how to convert an array to string using PHP.

I hope this article on convert array to string in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials