Conversion of arrays and strings in PHP is an important operation, because in actual operations, we often need to convert data from one format to another. In PHP, you can use a series of built-in functions to convert arrays and strings. In this article, we will focus on how to convert array to string and string to array in PHP.
1. Convert the array to a string
implode() function concatenates all elements in the array into a string. and returns a string. You can use the implode() function to convert an array into a string. Its most basic use is to concatenate an array into a string.
For example, we have an array containing multiple strings:
$arr = array('Hello', 'World', '!');
Okay Use the implode() function to concatenate the array into a string:
$str = implode(' ', $arr);
In this example, a space is used as the concatenator to Arrays are concatenated into a string. When using the implode() function to convert an array into a string, the choice of connectors is very important because they will determine the final string output format. As shown in the following example:
$arr = array('apple', 'orange', 'banana');
$str = implode(',', $arr);
echo $str; // apple,orange,banana
In this example, we use commas as the connector.
The join() function is the same as the implode() function. It can also concatenate the elements in the array into a string and return it. Actually the only difference between the two is the function name, so they do the same thing. For example:
$arr = array('apple', 'orange', 'banana');
$str = join('-', $arr);
echo $str; / / apple-orange-banana
The serialize() function serializes the entire array into a string. This string is a string that can be stored directly in a file or database. We can use the unserialize() function to deserialize this string into the original array. For example:
$arr = array('apple', 'orange', 'banana');
$str = serialize($arr);
echo $str;
In this example, the $str variable contains the serialized representation of the entire array.
The json_encode() function is a very powerful tool in PHP, which can convert a PHP array into a JSON format string. This string can be output directly to the browser, or used for front-end and back-end communication. For example:
$arr = array('apple', 'orange', 'banana');
$str = json_encode($arr);
echo $str;
In this example, the $str variable contains the JSON string representation of the array.
2. Convert a string into an array
explode() function decomposes a string into an array and returns a array. For example, we have a variable containing a comma-separated string:
$str = 'apple,orange,banana';
You can use the explode() function to convert the string into an array:
$arr = explode(',', $str);
print_r($arr);
The output result is:
Array
(
<code>[0] => apple [1] => orange [2] => banana</code>
)
In this example, the string is broken into an array using commas as delimiters.
The str_split() function splits a string into a set of characters, with each character stored in an array element. For example:
$str = 'Hello World';
$arr = str_split($string);
print_r($arr);
The output result is:
Array
(
<code>[0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => W [7] => o [8] => r [9] => l [10] => d</code>
)
In this example, the str_split() function is used to break the string into a character array.
The preg_split() function can decompose a string into an array based on a regular expression. For example:
$str = 'The quick brown fox.';
$arr = preg_split('/[\s,.] /', $str);
print_r( $arr);
The output result is:
Array
(
<code>[0] => The [1] => quick [2] => brown [3] => fox</code>
)
In this example, use the preg_split() function to The string is broken down into an array of words.
The unserialize() function is used to restore a variable containing a serialized string to the original PHP array. For example:
$str = 'a:3:{i:0;s:5:"apple";i:1;s:6:"orange";i:2;s:6:" banana";}';
$arr = unserialize($str);
print_r($arr);
The output result is:
Array
(
<code>[0] => apple [1] => orange [2] => banana</code>
)
In this example, we use a serialized string and use the unserialize() function to restore it to the original PHP array.
Summary
The above are various methods for converting arrays to strings and strings to arrays in PHP. Whether you are using the implode() function to concatenate arrays into strings, or using the explode() function to convert strings into arrays, they are both very important and practical skills. Hope this article is helpful to everyone.
The above is the detailed content of php array conversion characters. For more information, please follow other related articles on the PHP Chinese website!