in this post, We’ll convert the array elements into a string. We are going to convert the array to string using three methods.
Let’s convert array elements to string using PHP 7. We’ll discuss implode()
, json_encode()
and join()
method and create sample example to convert array to string.
The implode()
method is a PHP built-in function for joining the items of an array. We just need to pass two parameters, One is an array of strings and the second is a delimiter (string to be used between the pieces) of your response string.
The delimiter is used to join them together into one string.
Syntax:
implode (separator, array)
Let’s take a simple example –
$blog_arr = Array ("Hi,","I","am","phpflow","blog."); $str = implode(" ",$blog_arr); echo $str;
Output:
Hi, I am phpflow blog.
The json_encode()
function is a PHP inbuilt function that converts a PHP array or object to a JSON representation.
Syntax:
string json_encode( $value, $option, $depth )
The sample example:
$blog_arr = Array ("Hi,","I","am","phpflow","blog."); $str = json_encode($blog_arr); echo $str;
Output:
Hi, I am phpflow blog.
The join()
method is a PHP built-in function that connects an array of elements separated by a string.
Syntax:
string join( $separator, $array)
The sample example:
$blog_arr = Array ("Hi,","I","am","phpflow","blog."); $str = join(" ", $blog_arr); echo $str;
Output:
Hi, I am phpflow blog.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More