Home > Article > Backend Development > How to convert string to multidimensional array in php
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.
Recommendation: "PHP Video Tutorial"
php Convert string to two-dimensional array
First method:
<?php $a = 'a1|a2|a3|a4|b1|b2|b3|b4'; $arr = explode('|', $a); //print_r($arr); $test = array(); foreach($arr as $value) { //echo $value{0}.'<br>'; $test[$value{0}][] = $value; } echo "<pre class="brush:php;toolbar:false">"; print_r($test); echo ""; ?>
Second method:
array_chunk(explode('|', $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!