search
Homephp教程php手册php一维数据转多维

今天在论坛里看到有个朋友的需求,见证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);
?>

打印出来的数组为:
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
                (
                )
 
        )
 
)
看到这些评论我笑啦:







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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor