$v){//loop body}"; 2. In the loop body, use is_array( ) function determines whether the outer element is an array type. If so, use unset() to delete the first array type element, and use the break statement to jump out of the entire loop. The syntax is "if(is_array($v)){unset($arr [$k]);break;}"."/> $v){//loop body}"; 2. In the loop body, use is_array( ) function determines whether the outer element is an array type. If so, use unset() to delete the first array type element, and use the break statement to jump out of the entire loop. The syntax is "if(is_array($v)){unset($arr [$k]);break;}".">

Home  >  Article  >  Backend Development  >  How to remove the first subarray in a two-dimensional array in php

How to remove the first subarray in a two-dimensional array in php

青灯夜游
青灯夜游Original
2022-06-20 19:41:251661browse

Removal steps: 1. Use foreach to traverse the outer array elements of the two-dimensional array, the syntax is "foreach($arr as $k=>$v){//loop body}"; 2. In the loop body, use the is_array() function to determine whether the outer element is of array type. If so, use unset() to delete the first array type element, and use the break statement to jump out of the entire loop. The syntax "if(is_array($v )){unset($arr[$k]);break;}".

How to remove the first subarray in a two-dimensional array in php

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

php removes 2D Method of the first subarray in the array

##1. Use the foreach statement to loop through the outer array elements of the two-dimensional array

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

Traverse the given $arr array. In each loop, the value of the current array is assigned to $v and the key name is assigned to $k.

2. In the loop body, use the is_array() function to determine whether the outer element is an array type. If so, delete the first sub-array and jump out of the loop.

  • If so, use unset() to delete the first array type element, and use the break statement to jump out of the entire loop

  • if(is_array($v)){
     unset($arr[$k]);
     break;
    }

Complete implementation code:

<?php
header("content-type:text/html;charset=utf-8");
$arr = array(1,2,3,array(4,5,6),7,8,array(9,10)); 
var_dump($arr);
$sum=0;
foreach($arr as $k => $v){
	if(is_array($v)){
		unset($arr[$k]);
		break;
	}
}
var_dump($arr);
?>

How to remove the first subarray in a two-dimensional array in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to remove the first subarray in a two-dimensional 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