Php

Convert array to string in PHP 7

in this post, We’ll convert the array elements into a string. We are going to convert the array to string using three methods.

Array to String in PHP

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.

Convert Array to String Using implode()

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.

PHP Array to String Using json_encode()

The json_encode() function is a PHP inbuilt function that converts a PHP array or object to a JSON representation.

Related Post

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.

Array to String Using join()

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.

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

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

4 months ago

What is the Purpose of php_eol in PHP?

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

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories