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

青灯夜游
Release: 2023-03-16 17:00:02
Original
3355 people have browsed it

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;
Copy after login

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

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

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;
}
Copy after login

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 "数组不包含子数组";
}
?>
Copy after login

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!

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