Home php教程 php手册 php一维数据转多维

php一维数据转多维

Jun 21, 2016 am 08:48 AM

今天在论坛里看到有个朋友的需求,见证csdn的版主都不愧是大牛的角色存在啊!!!保存起来,方便以后需要的时候使用。


字符串为:

vcD4KPHByZSBjbGFzcz0="brush:java;">1 = 光电鼠标 2 = 机械鼠标 3 = 没有鼠标 1.1 = 黑色光电鼠标 1.2 = 红色光电鼠标 1.2.1 = 蓝牙红色光电鼠标
版主解答为:

<?php
$s =<<< TXT
1 = 光电鼠标
2 = 机械鼠标
3 = 没有鼠标
1.1 = 黑色光电鼠标
1.2 = 红色光电鼠标
1.2.1 = 蓝牙红色光电鼠标
TXT;
 
$res = array();
foreach(preg_split("/[\r\n]+/", $s) as $r) {
  list($k, $txt) = explode(" = ", $r);
  $p =& $res;
  foreach(explode(".", $k) as $v) {
    if(! isset($p[$v])) $p[$v] = array("txt" => $txt, "child" => array());
    $p =& $p[$v]["child"];
  }
}
 
print_r($res);
?>
Copy after login

打印出来的数组为:
Array
(
    [1] => Array
        (
            [txt] => 光电鼠标
            [child] => Array
                (
                    [1] => Array
                        (
                            [txt] => 黑色光电鼠标
                            [child] => Array
                                (
                                )
 
                        )
 
                    [2] => Array
                        (
                            [txt] => 红色光电鼠标
                            [child] => Array
                                (
                                    [1] => Array
                                        (
                                            [txt] => 蓝牙红色光电鼠标
                                            [child] => Array
                                                (
                                                )
 
                                        )
 
                                )
 
                        )
 
                )
 
        )
 
    [2] => Array
        (
            [txt] => 机械鼠标
            [child] => Array
                (
                )
 
        )
 
    [3] => Array
        (
            [txt] => 没有鼠标
            [child] => Array
                (
                )
 
        )
 
)
Copy after login
看到这些评论我笑啦:







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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)