$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
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;}".
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(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); ?>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!