How to remove keys from array in php
PHP is a widely used scripting language that occupies an important position in Web development. In PHP, array is a common data structure that can store multiple variables of the same type. However, sometimes we need to remove the keys in the array in the program and keep only the data values. So, how to achieve it? This article will detail how to remove keys from an array in PHP.
1. array_values() function
PHP provides an array_values() function that can remove the keys in the array and retain only the data values. The function of this function is to return a new array, and the indexes in the new array are consecutive integers. The sample code is as follows:
$arr1 = array( 'one' => '1', 'two' => '2', 'three' => '3' ); $arr2 = array_values($arr1); print_r($arr2);
After executing this code, the output result is:
Array ( [0] => 1 [1] => 2 [2] => 3 )
As you can see, through the array_values() function, we successfully removed the keys in the original array $arr1.
2. Unset function
In addition to using the array_values() function, we can also use the unset function to delete the keys of an array. The unset function is a built-in function in PHP that deletes variables, including keys in an array. The following is a sample code:
$arr1 = array( 'one' => '1', 'two' => '2', 'three' => '3' ); foreach($arr1 as $key => $value) { unset($arr1[$key]); } print_r($arr1);
After executing this code, the output result is:
Array ( [0] => 1 [1] => 2 [2] => 3 )
As you can see, our code successfully deleted all keys in the original array, leaving only data value.
Thinking:
The array_values function is implemented by taking out the KEY and redistributing the index, while unset is achieved by directly deleting the key value. The difference is quite obvious.
The above is the detailed content of How to remove keys from array in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.
