Home Backend Development PHP Tutorial Commonly used array functions in PHP

Commonly used array functions in PHP

Jul 06, 2018 am 09:34 AM
array function

This article mainly introduces the commonly used array functions in PHP, which has certain reference value. Now I share it with you. Friends in need can refer to it

1. array_fill(index,number,value ) Fill the array with values

Parameter description:

Index: The first index value of the returned array

Number: Specifies the number of elements to be inserted

 value: The value used to fill the array

<?php
$a1=array_fill(3,4,"blue");
print_r($a1);
?> 
Copy after login

Run result:

Array ( [3] => blue [4 ] => blue [5] => blue [6] => blue )

2. array_combine($keys,$values) merge array

Parameter description:

$keys: Key name array

$values: Key value array

<?php
$fname=array("Bill","Steve","Mark");
$age=array("60","56","31");

$c=array_combine($fname,$age);
print_r($c);
?>
Copy after login

Run result:

Array ( [Bill] => 60 [Steve] => 56 [Mark] => 31 )

3. array_intersect_key($arr1,$arr2,$arr3...) compares two or more The key names of arrays, return the intersection

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blue","d"=>"pink");

$result=array_intersect_key($a1,$a2);
print_r($result);
?>
Copy after login

Return result:

Array ( [a] => red [c] => ; blue )

4. array_shift($arr) deletes the first element in the array and returns the deleted element

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a);
print_r ($a);
?>
Copy after login

return Result:

redArray ( [b] => green [c] => blue )

5, array_walk($arr,function($value,$key){}) function Apply a callback function to each element in the array

To change the value in the array, you need to use a reference type&$value

<?php
function myfunction(&$value,$key)
{
$value="yellow";
}
$a=array("a"=>"red","b"=>"green","c"=>"blue");
array_walk($a,"myfunction");
print_r($a);
?>
Copy after login

to return the result :

Array ( [a] => yellow [b] => yellow [c] => yellow )
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Introduction to PHP namespaces

Introduction to PHP file programming

The above is the detailed content of Commonly used array functions 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 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)

Array functions in PHP8: efficient use of array_pad() Array functions in PHP8: efficient use of array_pad() May 16, 2023 pm 02:00 PM

PHP8 is the latest PHP version which provides many new functions and improved features, one of which is the array function array_pad(). In this article, we will explore the efficient use of array_pad() function. What is the array_pad() function? The array_pad() function can fill an array to a specified length and return the filled array. This function accepts three parameters: array_pad(array$array,int$leng

Array functions in PHP8: multiple uses of array_unique() Array functions in PHP8: multiple uses of array_unique() May 17, 2023 am 08:13 AM

Arrays are a very common data type in the PHP programming language. The unique thing about arrays is that they allow us to store multiple related variables at once, and these variables can be manipulated and processed efficiently. In PHP8, there are many useful array functions that can help us optimize our code, one of which is array_unique(). The function of array_unique() is to remove duplicate array elements and return a new array. This function can be used in many situations. Below we will

Detailed explanation of how to use array_diff_key() of PHP array function Detailed explanation of how to use array_diff_key() of PHP array function Jun 27, 2023 pm 05:18 PM

As a popular programming language, PHP's array functions are also very powerful. When you need to compare the key names of two arrays, you can use the array_diff_key() function. This function can help us find out the key names that are in the first array but do not exist in the second array, and compare the differences between the arrays. This article will introduce in detail how to use the array_diff_key() function. Basic usage of array_diff_key() function array_diff

How to use Go language array function to sum and return the result? How to use Go language array function to sum and return the result? Jul 31, 2023 pm 02:25 PM

How to use Go language array function to sum and return the result? The Go language provides a wealth of array operation functions, including functions for finding the sum of array elements. Use these functions to conveniently perform sum operations on arrays and return the results. This article will introduce how to use the array function of Go language to sum and return the result, with code examples. First, let’s take a look at arrays in Go language. An array is a data structure that stores a fixed-size sequence of elements. In Go language, the length of the array is fixed, and the type and element of the array

Array functions in PHP8: various operating techniques of array_slice() Array functions in PHP8: various operating techniques of array_slice() May 15, 2023 pm 10:43 PM

In PHP8, array is a very common data structure that is often used to store and process data. Among them, the array_slice() function is a powerful tool that can slice, intercept and split arrays. This article will introduce various operating techniques of this function to help developers make better use of it. 1. Slicing operation The most basic operation of the array_slice() function is slicing. It can obtain a part of the array by specifying the starting position and length. The sample code is as follows: $arr=a

Quick sort using array functions in PHP Quick sort using array functions in PHP Jun 16, 2023 am 08:54 AM

PHP is a very popular programming language and it is widely used for web development. In PHP, array is a very common data type and a very powerful data structure. Because of this, PHP provides many array functions to help developers handle and manipulate arrays. This includes the quick sort function, which helps us sort arrays quickly. Quick sort is a common sorting algorithm. Its basic idea is to divide an array into two sub-arrays, one smaller than the other, through comparison and exchange, and then recursively

PHP8.1 update: performance improvements for array and string functions PHP8.1 update: performance improvements for array and string functions Jul 08, 2023 am 08:25 AM

PHP8.1 update: Performance improvements for array and string functions The PHP programming language has continued to evolve and improve over time. The recently released version of PHP 8.1 brings many new features and performance enhancements, especially when it comes to array and string functions. These improvements allow developers to handle array and string operations more efficiently, improving overall performance and efficiency. Performance improvements of array functions In PHP8.1, array functions have been improved and optimized. Here are some important examples of performance improvements for array functions: (1

Array functions in PHP8: new application method of array_key_first() Array functions in PHP8: new application method of array_key_first() May 16, 2023 am 08:56 AM

On November 26, 2020, the PHP8 version was officially released, which brought many new features and bug fixes. One of the things that has attracted the attention of many developers is the new application method of the array function array_key_first(). In the past, we might have used the array_shift() function to get the key of the first element of an array. But there is a problem with this method, that is, NULL will be returned when the array is empty. In PHP8, we can use the array_key_first() function

See all articles