PHP array operation of of add delete query sorting and other functions

  • 2020-03-31 20:45:11
  • OfStack

Data increase, delete, query, sorting details

The addition of an array (data addition at the beginning and end (no number of bars) and data operation at any place in the middle).
2 ~ delete the array (the first and last data delete (not limited to the number of bars) and the middle arbitrary position of the data delete operations, conditions: to delete the middle position of the array, the value after the move forward, connected to the previous position).
Sort operations on data (sort operations, want efficiency).
4 to an array of query (the one is used in the data query, if met, will generate a new array, the new array is to satisfy the query conditions, conditions: query, not query a certain value, and query a certain value to satisfy a certain condition, for example: query a value in the array is greater than 5, all selected)


1. Arrays in PHP are essentially map structures that do not re-index indexed arrays by adding or removing elements.
2. Avoid mixing indexed and associative arrays
For the rest of the requirements, the search for the PHP manual should have the answer.
To be honest, I've never been able to remember that many functions.
3. Default bubble sort, heap sort, quicksort, insertion sort, dichotomy. It depends on your needs
4. Cycle again.

PHP arrays, while powerful, are also inefficient


Delete 2 array, to the string as the index, delete directly on the OK. The unset
Number indexed, unset removed, and then reset the array with array_values.
3 usort sort, can use the callback function itself to implement the algorithm. Efficiency depends on how well you write the algorithm.


1 - add to the array (add data at the beginning and end (no limit to the number of bars) and add data anywhere in the middle).
Arrar_unshift (add data to array header)
Array_push (add array to end of array)
Arrar_fill (add anywhere in the middle)
2 ~ delete the array (the first and last data delete (not limited to the number of bars) and the middle arbitrary position of the data delete operations, conditions: to delete the middle position of the array, the value after the move forward, connected to the previous position).
Array_shift (array header deletes data)
Array_pop (array for not deleting data)

Array_slice does not make any changes to the original array, I mean array_splice.
I just checked the manual again, and found that array_splice is very powerful, for any number of any position of the array to add or delete the operation, can be done with array_splice.
For the problem of reconstructing a numeric index from a non-hash array, array_merge and array_values can both be used

What is an array?
In the course of developing with PHP, sooner or later, you will need to create many similar variables.

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

The elements in the array have their own ids, so they can be easily accessed.

There are three types of arrays:
The numerical array
Array with numeric ID keys
An associative array
Each ID key in the array is associated with a value
Multidimensional array
An array of values containing one or more arrays
The numeric array stores each element with a numeric ID key.

You can use different methods 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";

You can use these ID keys in the script:
 
<?php 
$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

An associative array
An associative array, each of whose ID keys 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:
 
$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:
 
$ages['Peter'] = "32"; 
$ages['Quagmire'] = "30"; 
$ages['Joe'] = "34"; 

You can use the ID key in the script:
 
<?php 
$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 an automatically assigned ID key:
 
$families = array 
( 
"Griffin"=>array 
( 
"Peter", 
"Lois", 
"Megan" 
), 
"Quagmire"=>array 
( 
"Glenn" 
), 
"Brown"=>array 
( 
"Cleveland", 
"Loretta", 
"Junior" 
) 
); 

If you output the array, it should look something like this:
 
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 array above:

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



The basic concept
An array in PHP is actually an ordered graph. A diagram is a type that maps values to keys. So you can use PHP arrays as normal arrays, or you can use them to simulate dictionaries, collections, stacks, queues, trees, and many other data structures.
Array creation:
The general format for creating arrays is: $arrName = array([key =>]) Value,...). , where the key can be an integer or a string, and the value can be any value.
Example:
 
$arr1 = array('aaa', 'bbb'); 
$arr2 = array('a'=>'AAA', 'b'=>'BBB'); 

Create an empty array: $arr = array();
If the array does not exist, storing a value into the array creates the array, but retrieving a value from an undefined array does not.
 
$arr[0] = 'aaa'; 
$arr[1] = 'bbb'; 
foreach($arr as $v){ 
echo $v; 
} 

Operating an array
1: add a value to the end of the array
 
$arr = array('a'=>'AAA', 'b'=>'BBB'); 
$arr[] = 'CCC'; 

2: iterate through groups
Method 1
 
$arr = array('a'=>'AAA', 0=>'BBB'); 
foreach($arr as $a){ 
echo $a; 
} 

Method 2
$array = array (1, 2);
$count = count ($array);
For ($I = 0; $I < $count; ${i++)
Echo $array ($I);
}
3: empty the entire array
The unset ($arr);
4: empty the specified element
The unset ($arr [index]);
Common array function
Print_r ($arr);
View array information;
The unset ();
This function allows you to cancel the key name in an array. Note that the array will not rebuild the index.
$a = array(1 => 'one', 2 = > 'two', 3 = > "Three");
The unset ($a [2]);

The count () or the sizeof ()
Get the array size;
The array_pad ()
Fill the array
Bits and pieces
PHP arrays are divided into two types: indexed and associative. The key value of the indexed array is an integer, starting from 0; The key value of an associative array is a string.
Whether it is an indexed array or an associative array, the key value cannot be repeated. If repeated, the previous value will be overwritten.
$arr[1] references the same element as $arr['1'], but not the same element as arr['01'].
The index value of the associative array should be quoted in single or double quotes, and the absence of quotes in php5 results in an error. You cannot quote an array element in a string.
 
$arr['a'] = 'AAA'; 
$arr['b'] = 'BBB'; 
echo "array[a] is $arr[a]"; 

Related articles: