Home >Backend Development >PHP Problem >How to determine whether an array contains an array in php

How to determine whether an array contains an array in php

青灯夜游
青灯夜游Original
2022-08-02 20:33:303330browse

Steps: 1. Define a variable and assign it to 0, with the syntax "$f=0;"; 2. Use a foreach loop to traverse the outer array elements of the two-dimensional array, with the syntax "foreach(array as $ v){...}"; 3. In the loop body, determine whether the outer element is of array type. If so, set the value of "$f" to 1 and jump out of the loop. The syntax is "if(is_array($v )){$f=1;break;}"; 4. After the loop ends, determine whether the value of "$f" is 1. If so, the array contains a subarray.

How to determine whether an array contains an array in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php determines an array Steps to include subarrays

Step 1: Define a variable and assign the value to 0

$f=0;

Step 2: Use the foreach statement Loop through the outer array elements of the two-dimensional array

foreach($arr as $v){
//循环体代码
}

Traverse the given $arr array, and assign the current outer array value to $v in each loop.

Step 3: In the loop body, use the is_array() function to determine whether the outer element is an array type

  • If it is an array type, Then set the value of variable $f to 1 and use the break statement to jump out of the loop.

if(is_array($v)){
	$f=1;
	break;
}

Step 4: After the loop ends, determine whether the value of variable $f is 1

  • If 1, the array contains the subarray

  • If not 1, the array does not contain the subarray

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr = array(1,2,3,array(4,5,6),7,8,array(9,10)); 
var_dump($arr);
$f=0;
foreach($arr as $v){
	if(is_array($v)){
		$f=1;
		break;
	}
}
if($f==1){
	echo "数组包含子数组";
}else{
	echo "数组不包含子数组";
}
?>

How to determine whether an array contains an array in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine whether an array contains an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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