PHP processing array Common function summary

Source: Internet
Author: User
Tags arrays


The processing of arrays in PHP is one of the most common skills required. Summarizes several of the most common advanced functions for handling arrays.

1. Array_merge ()

Merges an array, merges the cells of one or more arrays, attaches the value in one array to the back of the previous array, and then returns an array.
Associative array: When there is the same string key name, the value after the key name overrides the previous value
Indexed array: The value that follows will not overwrite the original value, but append to the back

Array Array_merge (array array1 array2...,arrayn)
Example:

<?php
$a = Array ("A", "B", "C");
$b = Array ("1", "2", "3");
$c = Array_merge ($a, $b);
Print_r ($c);

Output results
Array ([0] => a [1] => b [2] => C [3] => 1 [4] => 2 [5] => 3)
?>

2. Array_merge_recursive ()

function with Array_merge (), except when you encounter the same value with different processing methods
Associative array: When there is the same string key name, the key value becomes an array, and the same key value is placed in an array

Example:

<?php
$a = Array ("A" => "123", "B" => "yes");
$b = Array ("C" => "No", "a" => "AAA");
$d = array_merge_recursive ($a, $b);
Print_r ($d);

Output results
Array ([a] => array ([0] => 123 [1] => AAA) [b] => yes [C] => No)
?>

3. Array_slice ()

Split index array, similar to the use of substr (), offset start cut, cut length so many, not set length is cut to the end

Array array_slice (array array, int offset[,int length])
Example:

<?php

$a = Array ("A", "B", "C", "D", "E", "F", "G");
$b = Array_slice ($a, 3);
Print_r ($b);

Output
Array ([0] => D [1] => E [2] => F [3] => G)
?>

4. Array_multisort ()

Returns the sorted array:

Array_multisort (array1,sorting order,sorting type,array2,array3 ...)

This function is powerful and has several uses:

To sort directly

<?php
$a 1=array ("Dog", "Cat");
$a 2=array ("Fido", "Missy");
Array_multisort ($a 1, $a 2);
Print_r ($a 1);
Print_r ($a 2);

Array ([0] => Cat [1] => Dog) Array ([0] => Missy [1] => Fido)
?>

Sort by setting Sort by

<?php
/* Set Reverse forward order * *
$a 1=array ("Dog", "Dog", "Cat");
$a 2=array ("Pluto", "Fido", "Missy");
Array_multisort ($a 1,sort_asc, $a 2,sort_desc);
Print_r ($a 1);
Print_r ($a 2);

Array ([0] => Cat [1] => Dog [2] => Dog) Array ([0] => Missy [1] => Pluto [2] => Fido)

/* Set reverse positive sequence, sorting standard * *
$a 1=array (1,30,15,7,25);
$a 2=array (4,30,20,41,66);
$num =array_merge ($a 1, $a 2);
Array_multisort ($num, sort_desc,sort_numeric);
Print_r ($num);

Array ([0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => 7 [8] => ; 4 [9] => 1)
?>

Two-dimensional arrays are sorted according to a key value

<?php
/* Time to sort the fields, Sort_desc in descending order, $all the array to be sorted * *
foreach ($all as $k => $v)
{
$time [] = $v [' Time '];
}
Array_multisort ($time, Sort_desc, $all);
?>

5. Array_intersect () and ARRAY_INTERSECT_ASSOC ()

Array_intersect () returns an array of reserved keys that consist of only the values that appear in the first array and that appear in each of the other input arrays.
ARRAY_INTERSECT_ASSOC () returns an array of key/value pairs that appear in the first array and also appear in all other input arrays.
Application scenario: Common friend recommendation

<?php
$a = Array ("Kevin", "Mike", "Jone");
$b = Array ("Jack", "Kevin", "Anna");
$c = Array ("Steven", "Json", "Kevin");
$output = Array_intersect ($a, $b, $c);
Print_r ($output);

Output
Array ([0] => Kevin)
?>

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.