Table of Contents
怎么由一维数组生成多维数组
Jun 06, 2016 pm 08:06 PM
php
一直不太明白,怎么能快捷的由一维数组生成多维数组:
我有一个字段是字符串,值可能是1或1-2或1-2-3-4-5...
例如如果是1-2-3-4,怎么能生成为多维数组:
<code>Array ( [1] => Array ( [2] => Array ( [3] => Array ( [4] => Array ( ) ) ) ) )</code>
Copy after login
Copy after login
多谢
回复内容:
一直不太明白,怎么能快捷的由一维数组生成多维数组:
我有一个字段是字符串,值可能是1或1-2或1-2-3-4-5...
例如如果是1-2-3-4,怎么能生成为多维数组:
<code>Array ( [1] => Array ( [2] => Array ( [3] => Array ( [4] => Array ( ) ) ) ) )</code>
Copy after login
Copy after login
多谢
// from https://segmentfault.com/q/1010000004647414 $str = '1-2-3-4'; $arr = explode('-', $str); $last = null; while($last = array_pop($arr)) { if(null != $last) { array_push($arr,[array_pop($arr)=>$last]); } if(count($arr) <= 1) { break; } } print_r($arr[0]); /*Array ( [] => Array ( [1] => Array ( [2] => Array ( [3] => 4 ) ) ) )*/
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 Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
