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

How to convert string to multidimensional array in php

藏色散人
Release: 2023-03-04 11:56:01
Original
3302 people have browsed it

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 "
"; ?>
Copy after login

Second method:

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

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!

Related labels:
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