Home Backend Development PHP Problem How to delete a value element in a one-dimensional array in php

How to delete a value element in a one-dimensional array in php

Aug 06, 2020 pm 01:27 PM
php one-dimensional array value

php method to delete a value element in a one-dimensional array: 1. Use the for loop statement and the function [array_merge()] to remove the value, the code is [$arr = array_merge($arr)]; 2. Use the function [array_splice] to automatically reset the sequence value.

How to delete a value element in a one-dimensional array in php

php method to delete a value element in a one-dimensional array:

1. Write it yourself for loop

Remove $tmpthe value of this element from array

1

2

3

4

5

6

7

8

9

10

<?php

$tmp = '324';

$arr = array(

'0' => '321',

'1' => '322',

'2' => '323',

'3' => '324',

'4' => '325',

'5' => '326',

);

Copy after login

code

1

2

3

4

5

foreach( $arr as $k=>$v) {

 if($tmp == $v) unset($arr[$k]);

}

print_r($arr);

?>

Copy after login

at this time

1

2

3

4

5

6

7

8

Array

(

 [0] => 321

 [1] => 322

 [2] => 323

 [4] => 325

 [5] => 326

)

Copy after login

To reset the index, add a sentence

1

2

3

4

5

6

foreach( $arr as $k=>$v) {

 if($tmp == $v) unset($arr[$k]);

}

$arr = array_values($arr);

print_r($arr);

?>

Copy after login

The result at this time

1

2

3

4

5

6

7

8

Array

(

 [0] => 321

 [1] => 322

 [2] => 323

 [3] => 325

 [4] => 326

)

Copy after login
Copy after login
Copy after login
Copy after login

array_merge() can also achieve the same effect

1

2

3

4

5

6

foreach( $arr as $k=>$v) {

 if($tmp == $v) unset($arr[$k]);

}

$arr = array_merge($arr);

print_r($arr);

?>

Copy after login

1

2

3

4

5

6

7

8

Array

(

 [0] => 321

 [1] => 322

 [2] => 323

 [3] => 325

 [4] => 326

)

Copy after login
Copy after login
Copy after login
Copy after login

2. Prioritize using PHP’s built-in functions, because they are implemented in C and are more efficient than writing them yourself.

Use array_search and array_splice, where array_splice automatically resets the sequence value.

1

2

3

$key=array_search($tmp ,$arr);

array_splice($arr,$key,1);

var_dump($arr);

Copy after login

Result at this time

1

2

3

4

5

6

7

8

Array

(

 [0] => 321

 [1] => 322

 [2] => 323

 [3] => 325

 [4] => 326

)

Copy after login
Copy after login
Copy after login
Copy after login

Best practice

1

2

$arr = array_merge(array_diff($arr, array($tmp)));

var_dump($arr);

Copy after login

Result

1

2

3

4

5

6

7

8

Array

(

 [0] => 321

 [1] => 322

 [2] => 323

 [3] => 325

 [4] => 326

)

Copy after login
Copy after login
Copy after login
Copy after login

Here, if the array elements are complex data structures, comparison can also be achieved. Of course the data itself is still one-dimensional.

In the above example, $tmp is a value. If $tmp is an array or other complex data structure, delete all elements contained in $tmp from $array. The above method is also valid

1

2

$arr = array_merge(array_diff($arr, $tmp));

var_dump($arr);

Copy after login

Related learning recommendations: php programming (video)

The above is the detailed content of How to delete a value element in a one-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

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 Article Tags

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 Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

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 Installation and Upgrade guide for Ubuntu and Debian

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

CakePHP File upload

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

CakePHP Routing

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

Discuss CakePHP

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

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

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

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles