Home Backend Development PHP Tutorial PHP array operations (add, delete, query, sort) and other function descriptions page 1/2_PHP tutorial

PHP array operations (add, delete, query, sort) and other function descriptions page 1/2_PHP tutorial

Jul 21, 2016 pm 03:37 PM
php function delete Increase right sort operate data array Inquire detailed illustrate

Detailed instructions for adding, deleting, querying, and sorting data

Adding to an array (adding data at the beginning and end (no limit on the number) and adding data anywhere in the middle).
2~ Deletion of the array (deletion of data at the beginning and end (no limit on the number of items) and data deletion at any position in the middle, condition: if the middle position of the array is deleted, the subsequent values ​​will be moved forward and connected to the previous position).
3~ Sorting operation of data (sorting operation requires efficiency).
4~ Query of array (query a certain data in the array, if satisfied, generate a new array, this new array It means to satisfy the query conditions. Condition: query, not to query a certain value, but also to query a certain value that satisfies a certain condition. For example: query a certain value in the array that is greater than 5 and filter out all)


1. Arrays in php are essentially map structures. For indexing arrays, adding or deleting elements does not re-index.
2. Avoid mixing index arrays and associative arrays
For the rest of the requirements, you should find the answer in the PHP manual.
To be honest, I have never been able to remember so many functions.
3. Default bubble sort, heap sort, quick sort, insertion sort, binary sort. It depends on your needs
4. It’s still a cycle.

Although PHP arrays are powerful, their efficiency is also very low


2 To delete an array, if it is indexed by a string, just delete it directly. unset
Number-indexed, unset deletes, and then uses array_values ​​to reset the array.
3 usort sorting, you can use the callback function to implement the algorithm yourself. The efficiency mainly depends on the algorithm you write.


1~ Adding to the array (adding data at the beginning and end (no limit on the number) and adding data anywhere in the middle).
arrar_unshift (adding data to the head of the array)
array_push( Add an array at the end of the array)
arrar_fill (add at any position in the middle)
2~ Deletion of the array (deletion of data at the beginning and end (no limit on the number) and deletion of data at any position in the middle, condition: deletion of data at the middle position of the array Deleted, the following values ​​are moved forward and connected to the previous position).
array_shift (the data is deleted from the head of the array)
array_pop (the data is not deleted from the array)

array_slice will not change the original Any modification to the array, I mean array_splice.
I checked the manual just now and found that array_splice is so powerful. You can use array_splice to add, delete, or modify any number at any position in the array.
Regarding the problem of rebuilding numeric indexes for non-hash arrays, both array_merge and array_values ​​can be used. After testing just now, the latter is faster

What is an array?
Sooner or later in the process of developing with PHP, you will need to create many similar variables.

Instead of having many similar variables, you can store data as elements in an array.

Elements in the array have their own IDs so they can be accessed easily.

There are three array types:
Numeric array
Array with numeric ID keys
Associative array
Each ID key in the array is associated with a value
Multidimensional array
Array containing one or more arrays Numeric array
Numeric array stores each element with a numeric ID key.

Different methods can be used to create numeric arrays:

Example 1
In this example, the ID key is automatically assigned:

$names = array("Peter","Quagmire","Joe");

Example 2
In this example, we manually assign the ID key:

$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";

can be used in scripts These ID keys:

Copy code The code is as follows:

$names[0] = " Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors";
?>

Output of the above code:
Quagmire and Joe are Peter's neighbors

Associative array
Associative array, each ID key is associated with a value.

Using numeric arrays is not the best practice when storing data about specifically named values.

With associative arrays, we can use values ​​as keys and assign values ​​to them.

Example 1
In this example, we use an array to assign ages to different people:
Copy code The code is as follows:

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

Example 2
This example is the same as Example 1, but shows another way to create an array:
Copy code The code is as follows:

$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages[' Joe'] = "34";

You can use the ID key in the script:
Copy the code The code is as follows:

$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

echo " Peter is " . $ages['Peter'] . " years old.";
?>

Output of the above script:

Peter is 32 years old.

Multidimensional array
In a multidimensional array, each element in the main array is also an array. Each element in a subarray can also be an array, and so on.

Example 1
In this example, we create a multidimensional array with automatically assigned ID keys:
Copy code The code is as follows:

$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);

If you output this array, it should look like this:
Copy code The code is as follows:

Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[ 0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)


Example 2
Let us try to display a single value from the above array:

echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?";
Output of the above code:
Is Megan a part of the Griffin family?

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321941.htmlTechArticleData addition, deletion, query, sorting details the addition of the array (adding head and tail data (no limit on the number of items)) And add data operations anywhere in the middle). 2~Delete the array (the first and last...
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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks 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 Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

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 Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

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.

See all articles