PHP array operation (add, delete, query, sort) etc function description 1th/2 page _php Tutorial

Source: Internet
Author: User
Data Add, delete, query, sort detailed description

Add data to an array (the data is added (without qualifying the number of bars) and anywhere in the middle.
2~ the deletion of the array (the number of data deletions (without qualifying bars) and the data deletion operation at any location in the middle, condition: Delete the middle position of the array, move forward, and connect to the previous position.
3~ the sort operation for the data (the sort operation, which is the efficiency).
4~ the query of an array (a query to a data in a group, if satisfied, a new array is generated, the new array is to satisfy the query criteria: query, not query a certain value, and query a certain value satisfies a condition, for example: query array of a value greater than 5, all filtered out)


1, the array in PHP is essentially the map structure, for an indexed array, add or remove elements, it does not re-index.
2. Avoid mixing indexed arrays and associative arrays
The rest of the requirements, find PHP manuals 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 what you need.
4. It's still a loop.

PHP arrays are powerful, but inefficient


2 The deletion of the array, indexed by the string, is directly deleted on OK. unset
Indexed by numbers, unset is deleted, and then the array is reset using Array_values.
3 Usort Sorting, you can use the callback function to implement the algorithm itself. Efficiency depends on how you write the algorithm.


1~ The addition of an array (the data is added (without qualifying the number of bars) and any position in the middle is added.
Arrar_unshift (array header add data)
Array_push (add array at tail of array)
Arrar_fill (add any position in the middle)
2~ the deletion of the array (the number of data deletions (without qualifying bars) and the data deletion operation at any location in the middle, condition: Delete the middle position of the array, move forward, and connect to the previous position.
Array_shift (array header delete data)
Array_pop (array is not to delete data)

Array_slice does not cause any changes to the original array, I want to say array_splice.
Just checked the next manual, found that the Array_splice function is good and powerful, any number of any location of the array of additions and deletions to operate, can be done with Array_splice.
On the issue of rebuilding a digital index on a non-hash array, both Array_merge and Array_values can, just after experimentation, the latter faster

what is an array?
You will need to create many similar variables in the process of using PHP for development, either early or late.

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

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

There are three types of arrays:
Array of values
An array with a numeric ID key
Associative arrays
Each ID key in the array associates a value
Multidimensional arrays
Array of valarray containing one or more arrays of numbers
Each element stored in a numeric array has a numeric 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 assign the ID key:

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

You can use these ID keys in your script:
Copy CodeThe code is as follows:
$names [0] = "Peter";
$names [1] = "quagmire";
$names [2] = "Joe";
echo $names [1]. "and". $names [2]. "Is". $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 ID key is associated with a value.

Using numeric arrays is not the best practice when storing data about a specific named value.

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 age to different people:
Copy CodeThe code is as follows:
$ages = Array ("Peter" =>32, "quagmire" =>30, "Joe" =>34);

Example 2
This example is the same as Example 1, but it shows another way to create an array:
Copy CodeThe code is as follows:
$ages [' Peter '] = "32";
$ages [' quagmire '] = "30";
$ages [' Joe '] = "34";

You can use the ID key in your script:
Copy CodeThe code is as follows:
$ages [' Peter '] = "32";
$ages [' quagmire '] = "30";
$ages [' Joe '] = "34";

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

Output from the above script:

Peter is the years old.

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

Example 1
In this example, we created a multidimensional array with an auto-assigned ID key:
Copy CodeThe 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 something like this:
Copy CodeThe 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's try to display 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?

http://www.bkjia.com/PHPjc/321941.html www.bkjia.com true http://www.bkjia.com/PHPjc/321941.html techarticle data additions, deletions, queries, sorting details the addition of the array (the addition of the data (without qualifying the number of bars) and the addition of data operations anywhere in the middle. 2~ the deletion of an array (...)

  • Related Article

    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.