php method to determine how many dimensions an array is: 1. The function determines the dimensions of the array by calling itself recursively, and uses the "foreach()" function to loop through all elements. If the result is still an array, add the dimensions. 1; 2. Convert the array into a JSON string through the "json_encode()" function, and then use regular expressions to determine the level depth of the JSON string.
The operating system of this tutorial: Windows10 system, PHP version 8.1.3, DELL G3 computer.
Methods to determine how many dimensions an array is:
Method 1: Use recursion to determine the array dimensions
The function in the example code below is called recursively Determine the dimension of the array by itself. If it is still an array after looping through all elements, add one to the dimension.
/** * 判断数组维度 * * @param array $arr * @return int */ function array_dimension($arr) { $dimension = 0; if (is_array($arr)) { foreach ($arr as $item) { if (is_array($item)) { $sub_dimension = array_dimension($item); if ($sub_dimension > $dimension) { $dimension = $sub_dimension; } } } $dimension++; } return $dimension; } // 示例数据 $arr1 = array(1, 2, 3); $arr2 = array(array(1, 2), array(3, 4)); $arr3 = array(array(array(1, 2), array(3, 4)), array(array(5, 6), array(7, 8))); echo '数组1的维度:' . array_dimension($arr1) . '<br>'; echo '数组2的维度:' . array_dimension($arr2) . '<br>'; echo '数组3的维度:' . array_dimension($arr3) . '<br>';
The above code output result:
数组1的维度:1 数组2的维度:2 数组3的维度:3
Method 2: Convert to JSON format to determine the array dimension
The function in the example code below converts the array into a JSON string, Then it is implemented by judging the hierarchical depth of the JSON string through regular expressions.
/** * 判断数组维度 * * @param array $arr * @return int */ function array_dimension($arr) { $json_arr = json_encode($arr, JSON_UNESCAPED_UNICODE); $max_depth = 1; if (preg_match_all('/(?:^|\{|\,)\s*(\[(?R)*\]|\{(?R)*\})\s*(?:(?=\:)|$)/', $json_arr, $m)) { foreach ($m[1] as $val) { $depth = substr_count(str_replace(["[", "{", "]", "}"], "", $val), ',') + 1; if ($depth > $max_depth) { $max_depth = $depth; } } } return $max_depth; } // 示例数据 $arr1 = array(1, 2, 3); $arr2 = array(array(1, 2), array(3, 4)); $arr3 = array(array(array(1, 2), array(3, 4)), array(array(5, 6), array(7, 8))); echo '数组1的维度:' . array_dimension($arr1) . '<br>'; echo '数组2的维度:' . array_dimension($arr2) . '<br>'; echo '数组3的维度:' . array_dimension($arr3) . '<br>';
The above code output result:
数组1的维度:1 数组2的维度:2 数组3的维度:3
The above are two commonly used methods to determine the array dimensions. They are relatively simple to implement. You can choose the appropriate method according to your own needs.
The above is the detailed content of How to determine how many dimensions an array is in php. For more information, please follow other related articles on the PHP Chinese website!

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

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

Hot Article

Hot Tools

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

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

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

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.
