PHP Common Array function usage Summary _php tips

Source: Internet
Author: User

This example describes the usage of PHP's common array functions. Share to everyone for your reference, specific as follows:

1.array Array_merge (Array $array 1 [, Array $array 2 [, $array]])

Function: Merges one or more arrays of cells, and the values in one array are appended to the previous array. Returns an array of results.

If the input array has the same string key name, the value following the key name overrides the previous value. However, if the array contains a numeric key name, the subsequent value will not overwrite the original value, but append to the back.

If only one array is given and the array is a numeric index, the key name is sequentially indexed.

Example 1: The array has the same string key name

$array 1=array (' Color ' => ' greed ', ' 3 ' =>8);
$array 2=array ("A", "Color" => "Red", ' 3 ' =>8);
Var_dump (Array_merge ($array 1, $array 2));

Output:

Array (4) {
  ["Color"]=> string (3) "Red"
  [0]=> Int (8)
  [1]=> string (1) "a"
  [2]=> int (8)
}

The key name repeats the color is overwritten, takes only the later value ' red ', but the numeric key name identical ' 3 ' is not overwritten, but attaches to the back.

2.array array_slice (array $array, int $offset, [, int $length [, bool $preserve _keys]])

function function: Returns a sequence of arrays in an array specified according to the offset and length parameters.

If offset is non-negative: Then the array starts at the offset from the start end. $array =array (1,2,3,4,5,6); Array_slice ($array, 2); equivalent (1,2,3,4,5,6); Red represents the captured array
If offset is negative: the array starts at the offset from the end. Array_slice ($array,-2); equivalent (1,2,3,4,5,6);
If length is positive: the number of intercepts that begin with offset. Array_slice ($array, 2,2); equivalent (1,2,3,4, 5,6);
If length is negative: Then the sequence terminates at such a distance from the end of the array; Array_slice ($array, 2,-1); equivalent (1,2,3,4,5, 6);
The default is: to the end of the array.

On the specific use of array_slice can also refer to the site in the previous "PHP array_slice function usage Examples"

3.array_map (callback $callback, array $arr 1[, array $ ...])

function function: Returns an array that contains the cells of all cells in the ARR1 after the callback action. The number of arguments accepted by callback should be consistent with the number of arrays passed to the Array_map () function.

<?php
function Cube ($n)
{return
  ($n * $n * $n);
}
$a = Array (1, 2, 3, 4, 5);
$b = Array_map ("cube", $a);
Print_r ($b);
? >

Output:

Array
(
  [0] => 1
  [1] => 8
  [2] =>
  [3] =>
  [4] =>
)

For more information on PHP-related content readers can view the site topics: "PHP operation Office Document tips summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course", "PHP string (string) Summary of usage, Getting Started tutorial on Php+mysql database operations, and summary of common PHP database operations techniques

I hope this article will help you with the PHP program design.

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.