PHP Common Array function Usage summary, PHP array Summary _php tutorial

Source: Internet
Author: User
Tags getting started with php

PHP Common Array function Usage summary, PHP array Summary


The examples in this article describe common use of array functions in PHP. Share to everyone for your reference, as follows:

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

function function: Merges the cells of one or more arrays, and the values in an 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 numeric key names, subsequent values will not overwrite the original values, but are appended to the back.

If only one array is given and the array is a numeric index, the key name is re-indexed in a sequential manner.

Example 1: An array with 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 duplicate color of the key name is overwritten, taking only the following value ' red ', while the number key name of the same ' 3 ' is not overwritten, but appended to the back.

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

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

If offset is non-negative: The array starts at the offset from the start side. $array =array (1,2,3,4,5,6); Array_slice ($array, 2); equivalent (3,4,5,6); Red represents the truncated array
If offset is negative: the array starts at the offset from end to offset. Array_slice ($array,-2); equivalent (1,2,3,4,5,6);
If length is positive: the number of intercepts starting with offset. Array_slice ($array, 2,2); equivalent (3,4, 5,6);
If length is negative: the sequence terminates so far away from the end of the array; Array_slice ($array, 2,-1); equivalent (3,4,5, 6);
The default is: to the end of the array.

About the specific use of array_slice can also refer to the previous article "PHP array_slice function usage examples in detail"

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

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

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

Output:

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

More readers interested in PHP related content can view this site topic: "PHP Operations Office Document Tips summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP Object-oriented Programming tutorial", "PHP string (string) Usage summary, "Getting Started with Php+mysql database operation" and "PHP common database Operation Skills Summary"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Php method for querying and deleting multiple columns of data in a database (using array functions)
    • PHP Array function array_key_exists () Summary
    • Introduction to PHP Common array functions
    • An explanation of PHP's XML-to-array function
    • The PHP object is converted to an array function (Recursive method)
    • PHP array_merge function Use a problem to be aware of
    • Php method for merging two arrays with the Array_merge () function
    • PHP array Merge Array_merge () function usage considerations
    • Analysis of the difference between PHP merged array + and Array_merge
    • PHP uses the Array_slice function to get a random array or the first few data
    • Example of array_slice function usage in PHP
    • Use of PHP Array_slice functions and detailed parameters
    • Use Array_map to get rid of PHP files, delete directories
    • PHP array_map () array function usage instructions

http://www.bkjia.com/PHPjc/1113744.html www.bkjia.com true http://www.bkjia.com/PHPjc/1113744.html techarticle PHP Common Array function Usage summary, PHP array Summary This article describes the common use of PHP array functions. Share to everyone for your reference, as follows: 1.array array_merge (array $a ...

  • 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.