Home Backend Development PHP Problem What are the php array modification functions?

What are the php array modification functions?

Jul 23, 2020 pm 01:54 PM
php

php array modification functions are: 1. [array_change_key_case] changes all key names in the array to all uppercase or lowercase; 2. [array_fill_keys] fills the array with the specified keys and values; 3. [array_fill] Fills the array with the given values.

What are the php array modification functions?

php array modification functions include:

1, array_change_key_case: Change the keys in the array Change all key names to all uppercase or lowercase. Accepts two parameters, the first is the array to be modified, and the second is the optional case conversion flag, indicating whether to modify the key name to uppercase or lowercase, the default is CASE_LOWER. If the input is not an array, false will be returned and a warning will be generated.

2, array_fill_keys: Fill the array with the specified keys and values, accepting two parameters, the first is the specified key array, the value of the array is the key name, and the second parameter is the value used to populate the array.

3, array_fill: Fill the array with the given value, accepts three parameters, the first is a positive integer, identifying the starting index value of filling, if it is a negative number, it is returned The first index of the array is a negative number, the subsequent indexes start from 0, and the second one is an integer, indicating the amount of filling, which must be greater than or equal to 0, otherwise a warning will be generated. The third parameter is the value used to fill the array.

4, array_flip: Exchange the keys and values ​​in the array. If the value in the array is not a legal key name, a warning will be generated, and the problematic key-value pair will not appear in the results. If the same value appears multiple times, the last key name that appears will be used as the exchanged value, and the previous one will be discarded. Returns null if the exchange fails.

5, array_pad: Fill a value into the array with the specified length. Accepts three parameters, the first is the array to be filled, the second is the size of the array after filling, and the third is the value used for filling. If the specified array size is an integer, it will be filled from the right. If it is a negative number, it will be filled from the left. If it is smaller than the size of the original array, it will not be filled. Up to 1048576 values ​​can be filled at once. What is returned is a copy of the first array.

6, array_replace: Replace the elements of the first array with the passed array, accepts any number of arrays, if a key exists in the first array and also exists in the second array , replaces the values ​​in the first array with the values ​​in the second array. If it does not exist in the first array but exists in the second array, the element will be created in the first array. If it only exists in the first array it will be left unchanged. If multiple arrays are passed, It will be processed in order, and the subsequent array will overwrite the previous values ​​of the same key. If an error occurs, null is returned, otherwise the replaced array is returned. array_replace is non-recursive and does not determine the type of the value in the first array but overwrites it directly.

7, array_replace_recursive: The only difference from array_replace is that array_replace_recursive is recursive, that is, it will determine the type of the value in the first array. If it is an array, it will replace it recursively. the values ​​in the array.

8, array_splice: Remove a certain part of the array and replace it with other values. It accepts four parameters. The first is the array reference to be operated on, and the second is the starting position. , the third is an optional length, the default is the length of the array. The fourth is an optional replacement unit, which defaults to an empty array. Key names in the array being operated on are not preserved. If the starting position is a positive number, it is calculated from front to back, starting from 0. If it is a negative number, it is calculated from back to front, starting from -1. If the length is not passed in, it defaults to all units from the starting position to the end of the array. If the length is a positive number, the specified length of units is removed from the starting position. If it is a negative number, it is moved forward from the starting position. Removes cells of the specified length. If 0, no cells are removed. If the replacement unit is an array, the removed unit is replaced with the unit in the array. If no unit is removed, the replacement unit is inserted at the specified starting position. If the replacement unit has only one unit, there is no need to add array(), unless the unit itself is an array, object, or null, and the return value is an array containing the deleted unit.

9, array_unique: Remove duplicate values ​​in the array, accepts two parameters, the first is the array to be deduplicated, and the second is the sort order identifier, PHP5. The default is SORT_REGULAR in 2.9 and SORT_STRING in other versions. First sort the element values ​​in the array, and then only retain the first key name encountered for each value, ignoring subsequent key names. It does not mean that the first key name of the same value before sorting will be retained. . Return the deduplicated array, retaining key names.

10, array_unshift: Insert one or more units at the beginning of the array. The units are inserted as a whole. The incoming units will maintain the same order. After insertion, all numerical keys The name will be recalculated from zero, and the string key name will remain unchanged. Returns the number of inserted array elements.

11, array_shift: Move the unit at the beginning of the array out of the array, move all units forward one bit, all numeric key names start counting from zero, the text key names remain unchanged, and the array length is reduced 1. Using this function will reset the pointer inside the array. A warning will be generated if a non-array value is passed in. If an empty array or illegal value is passed in, null will be returned.

12, array_pop: Pops and returns the last unit of the array, and the array length is reduced by 1. This function only accepts array references and cannot directly pass in the array. If it is an empty array, null is returned. A warning will be generated if a non-array value is passed in. Using this function will reset the pointer inside the array.

13, array_push: Push one or more units into the end of the array and increase the corresponding length. This function only accepts references to the array and cannot be passed directly into the array. A warning will be generated if a non-array value is passed in. The pointer inside the array will not be reset after using this function. The return value is the total number of cells in the array after insertion.

<?php
$ar1=array("a"=>"a","b"=>"b","c"=>"c","d"=>"d","e"=>"e","f"=>"f");
$ar2=array("a","b","d","f","g","h");
$ar3=array("a","c","g");
$ar4=array("a"=>"a","b"=>array("a"=>"a","b"=>"b","c"=>"c"),"c"=>"c");
$ar5=array("a"=>"1","b"=>array("c"=>"c","d"=>"d","b"=>"b","e"=>"e"),"c"=>array("c","b","a"));
$ar6=array(1,2,3,4,5,6);
$ar7=array("a","b","d","f","g","h","a","c","g","e");
$ar8=array(1=>"a","02"=>"b",3=>"e",4=>"04");
$ar9=array();
 
var_dump(array_change_key_case($ar1,CASE_UPPER));
var_dump(array_fill_keys($ar6,"a"));
var_dump(array_fill_keys($ar6,$ar3));
var_dump(array_fill(-2,3,"a"));
var_dump(array_fill(3,0,"a"));
var_dump(array_flip($ar2));
var_dump(array_pad($ar2,"-10","z"));
var_dump(array_replace($ar4,$ar5));
var_dump(array_replace_recursive($ar4,$ar5));
var_dump(array_splice($ar3,1,1,array("x","y","z")));
var_dump($ar3);
var_dump(array_unique($ar7));
var_dump(array_unshift($ar8,"e","f"));
var_dump($ar8);
next($ar8);
var_dump(key($ar8));
var_dump(array_shift($ar8));
var_dump($ar8);
var_dump(key($ar8));
var_dump(array_shift($ar9));
next($ar8);
var_dump(key($ar8));
var_dump(array_pop($ar1));
var_dump(key($ar8));
var_dump($ar8);
var_dump(array_pop($ar9));
next($ar8);
var_dump(key($ar8));
$ar1[]="e";
var_dump($ar8);
var_dump(key($ar8));
var_dump(array_push($ar8,"f","g"));
var_dump(key($ar8));
 
?>
Copy after login

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of What are the php array modification functions?. 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 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

Video Face Swap

Video Face Swap

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

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)

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

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles