调用
$arr = array(array(1,2=>array(1,2,3)),2=>array(1=>array(array(array())),2),3); echo GetArrLv($arr);
例如 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; } }
2.调it
$arr = array(array(1,2=>array(1,2,3)),2=>array(1=>array(array(array())),2),3); echo GetArrLv($arr);
以上就是返回数组维数(层数)的内容,更多相关内容请关注PHP中文网(www.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

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

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.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function