PHP Common array functions (1)

Source: Internet
Author: User

1. Array_fill (Index,number,value) fills an array with values

Parameter description:

Index: The first index value of the returned array

Number: Specifies how many elements to insert

Value: The values used to populate the array

<?php$a1=array_fill (3,4, "blue");p Rint_r ($a 1);? >

Operation Result:

Array ([3] = blue [4] = blue [5] = = Blue [6] = blue)

2, Array_combine ($keys, $values) merging arrays

Parameter description:

$keys: Key an array group

$values: Array of key values

<?php$fname=array ("Bill", "Steve", "Mark"), $age =array ("" "," "" "]," "" "), $c =array_combine ($fname, $age);p Rint_r ($ c);? >

Operation Result:

Array ([Bill] [Steve] = [Mark] + 31)

3, Array_intersect_key ($arr 1, $arr 2, $arr 3 ...) Compares the key names of two or more arrays, returning the intersection

<?php$a1=array ("A" and "Red", "b" = "green", "c" = "blue"), $a 2=array ("a" and "Red", "c" = "Blue", "d" = > "Pink"); $result =array_intersect_key ($a 1, $a 2);p Rint_r ($result);? >

return Result:

Array ([A] = red [c] = blue)

4. Array_shift ($arr) deletes the first element in the array and returns the deleted element

<?php$a=array ("A" and "Red", "b" = "green", "C" and "Blue"); Echo array_shift ($a);p rint_r ($a);? >

return Result:

Redarray ([b] = green [c] = blue)

5, Array_walk ($arr, function ($value, $key) {}) functions apply a callback function to each element in the array

Changing the values in the array requires a reference type & $value

<?phpfunction myfunction (& $value, $key) {$value = "yellow";} $a =array ("a" and "Red", "b" = "green", "c" = "blue"), Array_walk ($a, "myfunction");p Rint_r ($a); >

return Result:

Array ([A] = yellow [b] = yellow [c] = yellow)

PHP Common array functions (1)

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.