How to convert array into string in php

PHPz
Release: 2023-04-24 17:08:51
Original
996 people have browsed it

The array in PHP is a very commonly used data structure, which can be used to store a series of data. Sometimes we need to convert an array into a string, or a string into an array. This article will describe how to perform these conversion operations.

1. Convert array to string

  1. implode() function

implode() function can connect the elements in the array with strings. Returns the concatenated string.

Syntax:

implode(连接符, 数组);
Copy after login

The connector is the separator that connects array elements and can be any string. For example, use the "," delimiter to connect array elements:

$arr = array('apple','orange','banana');
$str = implode(',', $arr);
Copy after login

The result is:

"apple,orange,banana"
Copy after login
  1. json_encode() function

json_encode() function can Encode the array into JSON string format. JSON is a lightweight data exchange format that is easy to read and write. It is one of the commonly used data formats in Web API.

Grammar:

json_encode(数组);
Copy after login

For example:

$arr = array('name'=>'小明','age'=>18,'gender'=>'male');
$str = json_encode($arr);
Copy after login

The result is:

"{"name":"小明","age":18,"gender":"male"}"
Copy after login

2. Convert string to array

  1. explode ()Function

explode() function can split a string into an array according to the specified delimiter.

Syntax:

explode(分隔符, 字符串);
Copy after login

For example, use the "," delimiter to split the string into an array:

$str = "apple,orange,banana";
$arr = explode(',', $str);
Copy after login

The result is:

array('apple','orange','banana')
Copy after login
  1. json_decode() function

json_decode() function can decode JSON format string into PHP array format.

Syntax:

json_decode(JSON字符串, 是否转换为关联数组);
Copy after login

By default, the json_decode() function returns a dummy object (stdClass object). You can set the second parameter to TRUE and an array will be returned.

For example:

$str = '{"name":"小明","age":18,"gender":"male"}';
$arr = json_decode($str, TRUE);
Copy after login

The result is:

array('name'=>'小明','age'=>18,'gender'=>'male')
Copy after login

Summary

This article introduces how to convert arrays to strings and strings to arrays in PHP method. Each method has its own characteristics and uses, and can be selected and used according to different needs. By mastering these methods, you can bring convenience and efficiency to PHP development work.

The above is the detailed content of How to convert array into string in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template