Home Backend Development PHP Tutorial PHP deletes null values ​​or specified value operations in an array_PHP tutorial

PHP deletes null values ​​or specified value operations in an array_PHP tutorial

Jul 13, 2016 am 10:55 AM
php introduce value about delete us designation operate array of null value

We introduce the operations of arrays in PHP, such as deleting specified values ​​in the array or determining whether there is a value in the array or clearing null values. Students in need can refer to it.

First, let’s take a look at the various methods on how PHP operates null values ​​in arrays

implode(); For this function, please refer to http://www.bKjia.c0m/phper/29/2dc95be9381b4bb1753083c09fda1a36.htm
Use implode() to output the array as a string and determine whether the output string is empty. At first glance, it seems to be a good method, but unfortunately, like the previous point, it does not work for arrays with more than two dimensions. For example:

The code is as follows
 代码如下 复制代码

$arr= array(array(),array(),array());
$str = implode(',',$arr);

if(empty($str)) echo "空";
else echo "非空";

Copy code


$arr= array(array(),array(),array());
 代码如下 复制代码

$arr= array("","","");
echo count($arr);

$str = implode(',',$arr);

if(empty($str)) echo "empty";
 代码如下 复制代码
$arr= array("d","s","");
echo in_array('', $arr);
else echo "not empty";



 代码如下 复制代码

$arr= array("","","");
if(empty($arr)) echo "空";
else echo "非空";

Obviously $arr is a two-dimensional array containing three empty arrays, which should be considered empty, but the output is indeed non-empty. Judgment failed.

3. count(); Please refer to http://www.bKjia.c0m/w3school/php/func_array_count.htm

echo count($arr);
The code is as follows Copy code

 代码如下 复制代码

function array_remove_key($array, $keys)
{
$num = count($keys);
$num_last = $num - 1;
$this_array_0 = &$array;
$last_key = $keys[$num_last];
for ($i = 0; $i < $num_last; $i++)
{
$this_key = $keys[$i];
$this_var_name = 'this_array_' . $i;
$next_var_name = 'this_array_' . ($i + 1);
if (!array_key_exists($this_key, $$this_var_name)) {
break;
}
$$next_var_name = &${$this_var_name}[$this_key];
}
unset(${$next_var_name}[$last_key]);
return $array;
}

$arr= array("","","");
4. in_array('', $arr)); For function usage, please refer to http://www.bKjia.c0m/phper/24/c5b81a8af14b1c0928eea343f59b454a.htm
The code is as follows Copy code
$arr= array("d"," s",""); echo in_array('', $arr); This can only show that there are empty elements in the array, but cannot prove that the array is empty. Obviously not. 5. empty(); For function usage, please refer to http://www.bKjia.c0m/so/php+empty() This cpyeh feels similar to the previous methods
The code is as follows Copy code
$arr= array("","",""); if(empty($arr)) echo "empty"; else echo "not empty"; The result is still non-empty 6. Use strlen(). If there is no content, the length seems to be 1 Combined with the above example, we write a complete deletion of empty elements in the array
The code is as follows Copy code
function array_remove_key($array, $keys) { $num = count($keys); $num_last = $num - 1; $this_array_0 = &$array; $last_key = $keys[$num_last]; for ($i = 0; $i < $num_last; $i++) { $this_key = $keys[$i]; $this_var_name = 'this_array_' . $i; $next_var_name = 'this_array_' . ($i + 1); if (!array_key_exists($this_key, $$this_var_name)) { break; } $$next_var_name = &${$this_var_name}[$this_key]; } unset(${$next_var_name}[$last_key]); return $array; }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632230.htmlTechArticleWe introduce the operation of arrays in php, delete the specified value in the array or determine whether there is a value in the array or clear the empty Value operation, students in need can refer to it. First let’s take a look at...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles