How to determine how many dimensions an array is in php

百草
Release: 2023-07-12 15:55:38
Original
1480 people have browsed it

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.

How to determine how many dimensions an array is in php

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) . '
'; echo '数组2的维度:' . array_dimension($arr2) . '
'; echo '数组3的维度:' . array_dimension($arr3) . '
';
Copy after login

The above code output result:

数组1的维度:1
数组2的维度:2
数组3的维度:3
Copy after login
Copy after login

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) . '
'; echo '数组2的维度:' . array_dimension($arr2) . '
'; echo '数组3的维度:' . array_dimension($arr3) . '
';
Copy after login

The above code output result:

数组1的维度:1
数组2的维度:2
数组3的维度:3
Copy after login
Copy after login

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!

Related labels:
source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!