Php array operations (add, delete, query, sort) and other functions description page 1/2 _ php skills

Source: Internet
Author: User
Add, delete, query, and sort php arrays. For more information, see. Detailed descriptions of data addition, deletion, query, and sorting

Add an array (add the first and last Data Records (not Limited) and add data anywhere in the middle ).
2 ~ Delete an array (data at the beginning and end of the array (no limit on the number of items) and any data in the middle to delete the data. condition: delete the data in the middle of the array and move the values following it forward, connect to the previous location ).
3 ~ Sort data (sort operations require efficiency ).
4 ~ Query the array (query a certain data in the array. If yes, a new array is generated. the new array meets the query conditions. condition: query, not to query a value, but to query a value that meets a certain condition. for example, to query all the values with a value greater than 5 in an array, filter them out)


1. the array in php is essentially a map structure. for an index array, add or delete elements without re-indexing.
2. avoid mixed index arrays and associated arrays
For other requirements, the php manual should have answers.
To be honest, I can't remember so many functions.
3. Default bubble sorting, heap sorting, fast sorting, insert sorting, and binary sorting. Check your requirements.
4. loop.

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


2. if the array is deleted and indexed, it is OK to delete it directly. Unset
If it is indexed by numbers, unset will be deleted and the array will be reset using array_values.
3 usort sorting. you can use the callback function to implement the algorithm. The efficiency mainly depends on the algorithm you write.


1 ~ Add an array (add the first and last Data Records (not Limited) and add data anywhere in the middle ).
Arrar_unshift (adding data to the array header)
Array_push (add an array at the end of the array)
Arrar_fill (any position in the middle)
2 ~ Delete an array (data at the beginning and end of the array (no limit on the number of items) and any data in the middle to delete the data. condition: delete the data in the middle of the array and move the values following it forward, connect to the previous location ).
Array_shift (delete data from the array header)
Array_pop (the array does not delete data)

Array_slice will not make any changes to the original array. I want to say array_splice.
I checked the manual and found that array_splice is powerful. you can use array_splice to add, delete, modify, and delete any number of arrays at any position.
Array_merge and array_values can both be used to reconstruct the digital index of a non-hash array. after the experiment, the latter is faster.

What is an array?
When using PHP for development, you need to create many similar variables either early or late.

Without many similar variables, you can store data as elements in an array.

All elements in the array have their own IDs, so they can be easily accessed.

There are three types of arrays:
Numeric array
Array with digit ID key
Join array
Each ID key in the array is associated with a value.
Multi-dimensional array
Array Value array that contains one or more arrays
Each element stored in the value array has a digital ID key.

You can use different methods to create a numeric array:

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

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

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

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

You can use these ID keys in the script:

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

Join array
Join array. each ID key is associated with a value.

It is not the best practice to use a numeric array to store data related to specific named values.

By associating arrays, we can use values as keys and assign values to them.

Example 1
In this example, we use an array to allocate ages to different people:

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 method to create an array:

The code is as follows:


$ Ages ['Peter '] = "32 ";
$ Ages ['quagmire'] = "30 ";
$ Ages ['job'] = "34 ";


You can use the ID key in the script:

The code is as follows:


$ Ages ['Peter '] = "32 ";
$ Ages ['quagmire'] = "30 ";
$ Ages ['job'] = "34 ";

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


Output of the above script:

Peter is 32 years old.

Multi-dimensional array
In a multi-dimensional array, each element in the main array is also an array. Each element in the sub-array can also be an array, and so on.

Example 1
In this example, we create a multidimensional array with an automatically assigned ID key:

The code is as follows:


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


If this array is output, it should be similar to the following:

The code is as follows:


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



Example 2
Let's try to display a single value in the above array:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.