Table of Contents
返回数组维数(层数)
调用
$arr = array(array(1,2=>array(1,2,3)),2=>array(1=>array(array(array())),2),3); echo GetArrLv($arr);
Copy after login
Copy after login
例如 implode 一个多维数组会报错,当然我们可以用类似 array_search(true, array_map('is_array', $arr)) 这样的方法判断是否2维+(性能控请Hold住这只是个比方)。
下面,我们用 print_r 作为媒介。
/** * 返回数组维数(层级) * @author echo* @param array $arr * @return int */ function GetArrLv($arr) { if (is_array($arr)) { #递归将所有值置NULL,目的1、消除虚构层如array("array(\n ()"),2、print_r 输出轻松点, array_walk_recursive($arr, function(&$val){ $val = NULL; }); $ma = array(); #从行首匹配[空白]至第一个左括号,要使用多行开关'm' preg_match_all("'^\(|^\s+\('m", print_r($arr, true), $ma); #回调转字符串长度 //$arr_size = array_map('strlen', current($ma)); #取出最大长度并减掉左括号占用的一位长度 //$max_size = max($arr_size) - 1; #数组层间距以 8 个空格列,这里要加 1 个是因为 print_r 打印的第一层左括号在行首 //return $max_size / 8 + 1; return (max(array_map('strlen', current($ma))) - 1) / 8 + 1; } else { return 0; } }
Copy after login
2.调it
$arr = array(array(1,2=>array(1,2,3)),2=>array(1=>array(array(array())),2),3); echo GetArrLv($arr);
Copy after login
Copy after login
以上就是返回数组维数(层数)的内容,更多相关内容请关注PHP中文网(www.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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
2 weeks ago
By DDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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
CakePHP Tutorial
1378
52

