Home >Backend Development >PHP Problem >How to convert string to multidimensional array in php

How to convert string to multidimensional array in php

藏色散人
藏色散人Original
2020-08-13 09:54:143298browse

How to convert a string into a multi-dimensional array in php: first define a string that needs to be converted into a multi-dimensional array; then split the string through the explode function and return an array composed of strings; finally pass "print_r" Just output the conversion result.

How to convert string to multidimensional array in php

Recommendation: "PHP Video Tutorial"

php Convert string to two-dimensional array

First method:

<?php
$a = &#39;a1|a2|a3|a4|b1|b2|b3|b4&#39;;
$arr = explode(&#39;|&#39;, $a);
//print_r($arr);
$test = array();
foreach($arr as $value)
{
    //echo $value{0}.&#39;<br>&#39;;
    $test[$value{0}][] = $value;
}
echo "<pre class="brush:php;toolbar:false">";
print_r($test);
echo "
"; ?>

Second method:

array_chunk(explode(&#39;|&#39;, $a), 4);

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

Statement:
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