Php code _ PHP Tutorial for deleting arrays and 2D array values

Source: Internet
Author: User
Delete php code for arrays and 2D array values. The method is as follows: $ arrarray (); assume that an array with 10 thousand elements contains repeated elements. $ Arrarray_flip (array_flip ($ arr); this allows you to delete duplicate elements. The method is as follows:
$ Arr = array (............) ; // Assume that an array with 10 thousand elements contains repeated elements.
$ Arr = array_flip ($ arr); // you can delete duplicate elements.

What is going on? Let's take a look at the role of array_flip (): array_flip () is used to exchange the keys and values of each element of an array, for example:
$ Arr1 = array ("age" => 30, "name" => "Happy Park ");
$ Arr2 = array_flip ($ arr1); // $ arr2 is array (30 => "age", "Fast Park" => "name ");
In the PHP array, different elements can take the same value, but the same key name cannot be used by different elements, such:
$ Arr1 = array ("age" => 30, "name" => "Happy Park", "age" => 20 ); "age" => 20 will replace "age" => 30
$ Arr1 = array ("name" => "Happy Park", "age" => 45 );
Here $ arr1 and $ arr2 are equal.
So we can know why array_flip ($ arr) can delete repeated elements in the array. First, the value in $ arr is changed to the key name, because the value is repeated. after the value is changed to the key name, the duplicate values become duplicate key names, the PHP engine deletes duplicate key names and retains only the last one. For example:
$ Arr1 = array ("age" => 30, "name" => "Happy Park", "age" => 20 );
$ Arr1 = array_flip ($ arr1); // $ arr1 is changed to array ("Fast Park" => "name", 20 => "age ");
// Restore the key name and value of $ arr1:
$ Arr1 = array_flip ($ arr1 );

The code above is concise: $ arr1 = array_flip ($ arr1 ));

I wrote an article about array deduplication, but it is limited to one-dimensional arrays. The following functions can be used for two-dimensional arrays:
The code is as follows:
// Remove duplicate values from two-dimensional arrays

Function array_unique_fb ($ array2D)
{
Foreach ($ array2D as $ v)
{
$ V = join (",", $ v); // dimension reduction. you can also use implode to convert a one-dimensional array to a string connected with commas (,).
$ Temp [] = $ v;
}
$ Temp = array_unique ($ temp); // remove the duplicate string, that is, the duplicate one-dimensional array.
Foreach ($ temp as $ k => $ v)
{
$ Temp [$ k] = explode (",", $ v); // re-assemble the split array
}
Return $ temp;
}

If you want to retain the key value of the array, you can use the following function:
The code is as follows:
// Remove duplicate values from the two-dimensional array and retain the key value

Function array_unique_fb ($ array2D)
{
Foreach ($ array2D as $ k => $ v)
{
$ V = join (",", $ v); // dimension reduction. you can also use implode to convert a one-dimensional array to a string connected with commas (,).
$ Temp [$ k] = $ v;
}
$ Temp = array_unique ($ temp); // remove the duplicate string, that is, the duplicate one-dimensional array.
Foreach ($ temp as $ k => $ v)
{
$ Array = explode (",", $ v); // re-assemble the split array
$ Temp2 [$ k] ["id"] = $ array [0];
$ Temp2 [$ k] ["litpic"] = $ array [1];
$ Temp2 [$ k] ["title"] = $ array [2];
$ Temp2 [$ k] ["address"] = $ array [3];
$ Temp2 [$ k] ["starttime"] = $ array [4];
$ Temp2 [$ k] ["endtime"] = $ array [5];
$ Temp2 [$ k] ["classid"] = $ array [6];
$ Temp2 [$ k] ["ename"] = $ array [7];
}
Return $ temp2;
}

This is probably the case.
Two-dimensional array deduplication
The code is as follows:

$ Arr = array (
Array ('id' => 1, 'name' => 'AAA '),
Array ('id' => 2, 'name' => 'BBB '),
Array ('id' => 3, 'name' => 'CCC '),
Array ('id' => 4, 'name' => 'ddd '),
Array ('id' => 5, 'name' => 'CCC '),
Array ('id' => 6, 'name' => 'AAA '),
Array ('id' => 7, 'name' => 'BBB '),
);
Function assoc_unique (& $ arr, $ key)
{
$ RAr = array ();
For ($ I = 0; $ I {
If (! Isset ($ rAr [$ arr [$ I] [$ key])
{
$ RAr [$ arr [$ I] [$ key] = $ arr [$ I];
}
}
$ Arr = array_values ($ rAr );
}
Assoc_unique (& $ arr, 'name ');
Print_r ($ arr );
?>


Use php built-in functions

Description


Array array_unique (array $ array)
Array_unique () accepts array as input and returns a new array without repeated values.

Note that the key name remains unchanged. Array_unique () first sorts the values as strings, then retains only the first key name that is encountered for each value, and then ignores all the key names that follow. This does not mean that the first key name that appears for the same value in the unordered array will be retained.

Note: when and only when (string) $ elem1 = (string) $ elem2, the two units are considered the same. That is, when the expression of a string is the same. The first unit will be retained.


Example #1 array_unique () Example

$ Input = array ("a" => "green", "red", "B" => "green", "blue", "red ");
$ Result = array_unique ($ input );
Print_r ($ result );
?>
The above example will output:

Array
(
[A] => green
[0] => red
[1] => blue
)


Example #2 array_unique () and type

$ Input = array (4, "4", "3", 4, 3, "3 ");
$ Result = array_unique ($ input );
Var_dump ($ result );
?>
The above example will output:

Array (2 ){
[0] => int (4)
[2] => string (1) "3"
}

Related tutorials
Http://www.bKjia. c0m/phper/php-function/34794.htm

Arrays $ arr = array (); // suppose there are an array of 10 thousand elements with repeated elements. $ Arr = array_flip ($ arr); // you can delete duplicate elements. Exactly...

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.