PHP array Operations (ADD, delete, query, sort) function Description of the 1th/2 page _php tips

Source: Internet
Author: User
Tags numeric
data increase, delete, query, sort details

The addition of an array (the top and end data are added (without limiting the number of bars) and the data operation is added anywhere in the middle).
2~ the deletion of the array (the end of data deletion (not qualifying), as well as data deletion at any point in the middle, conditions: the middle position of the array is deleted, the following values move forward, followed by the previous position.
3~ sort operations on data (sorting operations, to be efficient).
4~ an array of queries (one of the data in an array, if satisfied, then generate a new array, the new array is to meet the query conditions, conditions: query, not query a certain value, and query a certain value to meet a certain condition, for example: query array of a value greater than 5, all filtered out)


1, in PHP, the array is essentially a map structure, for indexed arrays, add or delete elements, it does not index.
2. Avoid mixed index arrays and associative arrays
The rest of the requirements to find the PHP manual should have an answer.
To tell you the truth, I can't remember so many functions.
3. Default bubble sort, heap sort, quick sort, insert sort, two points. Look at your needs.
4. or the cycle.

PHP arrays are powerful, but inefficient


2 The deletion of the array, with the string indexed, the direct delete is OK. unset
Numeric-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 itself. Efficiency mainly depends on how you write the algorithm.


1~ The addition of an array (the data is added (without limiting the number of bars) and any position in the middle to add data operations).
Arrar_unshift (Add data to array header)
Array_push (add array at the end of array)
Arrar_fill (added at any point in the middle)
2~ the deletion of the array (the end of data deletion (not qualifying), as well as data deletion at any point in the middle, conditions: the middle position of the array is deleted, the following values move forward, followed by the previous position.
Array_shift (array header delete data)
Array_pop (array is not deleting data)

Array_slice will not cause any changes to the original array, I want to say array_splice.
Just looked at the next manual, found that the Array_splice function is very powerful, the number of any location of the array of additions and deletions can be done with Array_splice.
About the problem of rebuilding a digital index with a non-hash array, both Array_merge and Array_values can, just after experimenting, the latter faster

what is an array?
In the process of developing using PHP, sooner or later, you will need to create many similar variables.

Without a lot of similar variables, you can store the data as an element in an array.

The elements in an array have their own IDs, so you can easily access them.

There are three types of arrays:
Numeric array
An array with a numeric ID key
Associative arrays
Each ID key in the array associates a value
Multidimensional arrays
An array of array values containing one or more arrays
The numeric array stores each element with a numeric ID key.

You can use different methods to create an array of values:

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

$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";

You can use these ID keys in your scripts:
Copy Code code as follows:

<?php
$names [0] = "Peter";
$names [1] = "quagmire";
$names [2] = "Joe";
echo $names [1]. "and". $names [2]. "Are". $names [0]. "' s Neighbors";
?>

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

Associative arrays
An associative array in which each of its ID keys is associated with a value.

Using a numeric array is not the best way to store data about a specific named value.

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

Example 1
In this case, we use an array to assign age to different people:
Copy Code code 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 code as follows:

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

You can use the ID key in your script:
Copy Code code as follows:

<?php
$ages [' Peter '] = "32";
$ages [' quagmire '] = "30";
$ages [' Joe '] = "34";

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

Output from the above script:

Peter is years old.

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

Example 1
In this example, we created a multidimensional array with an automatically assigned ID key:
Copy Code code 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 something like this:
Copy Code code 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's try to show a single value in the above array:

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

Current 1/2 page 12 Next read the full text

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.