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

WBOY
Release: 2016-07-21 15:37:18
Original
776 people have browsed it

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...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!