Simple examples of common arrays

Source: Internet
Author: User

<?php
/**
* @author GF
* TIME:2015/12/23
* Common Array Summary and usage
*/
/*********************************** Array Key-name key-value operation function start**********************************************/
$array = Array (
' name ' = ' Gaofei ',
' pasd ' = ' 123445 ',
' email ' = ' 825482 ',
);
header (' content-type:text/html;charset= ' Utf-8 "');
/* Get all the values of the array array_values (array)
Var_dump (Array_values ($array)); * *

/* Get the array's key name Array_keys (array)
Var_dump (Array_keys ($array)); * *

/* Retrieve in array if there is a value in_array (' parameter ', array)
if (In_array (' Gaofei ', $array))
{
Echo ' array exists in Gaofei ';
}
Else
{
Echo ' array does not exist in Gaofei ';
}*/

/*array_search (parameter, array) retrieves the parameter in the array, if present and returns the key name
var_dump (Array_search (' Gaofei ', $array)); * *

/*array_key_exists (parameter, array) retrieves whether the given key name exists in the array
if (array_key_exists (' name ', $array))
{
Echo ' array exists key name name ';
}
Else
{
Echo ' array does not exist with the key name name ';
}*/

/*isset retrieves whether the given key name exists in the array
Echo isset ($array [' SSS '])? ' This key name ' is present in the array ': ' The key name does not exist in the array ';

/*current Returns the current cell of the array initially pointing to the first cell of the array
Echo current ($array); */
/*********************************** Array Key-name key-value operation function start**********************************************/
/********************************** the fragment and fill of an array start**********************************************/
$arr = Array (
' name ' = ' Gaofei ',
' word ' = ' 123456 ',
' mail ' = ' wwwwww ',
);
$arr 2=array (0=> "Tiger",1=> "Lion");
/*array_splice (array, offset, length, substitution array) array is required, offset can be empty, length can be empty, new array is selected
Array_splice ($arr, 0, ' 1 ', $arr 2);
Print result Array (4) {[0]=> string (5) "Tiger" [1]=> string (4) "Lion" ["word"]=> string (6) "123456" ["Mail"]=> St Ring (6) "Wwwwww"}*/

/* Array_slice (array, offset, length) can take out any segment of the array, this function ignores the key name
$arrtwo = Array (0,1,2,3,4);
Var_dump (Array_slice ($arr, 0,1));
Print result Array (1) {["Name"]=> string (6) "Gaofei"}*/

/* Split array array_chunk (array, parameter, TRUE or FALSE) Note: True to preserve the original array key name, false opposite
Var_dump (Array_chunk ($arr, 2, true));
Print Results
Array (2) {
[0]=>
Array (2) {
["Name"]=>
string (6) "Gaofei"
["word"]=>
string (6) "123456"
}
[1]=>
Array (1) {
["Mail"]=>
string (6) "Wwwwww"
}
}*/
/*var_dump (Array_chunk ($arr, 2, false));
Print Results
Array (2) {
[0]=>
Array (2) {
[0]=>
string (6) "Gaofei"
[1]=>
string (6) "123456"
}
[1]=>
Array (1) {
[0]=>
string (6) "Wwwwww"
}
}*/

/* Fill Array_pad (array, length, padding) for the array
Var_dump (Array_pad ($arr, 7, ' 123 ')); * *

merging of/********************************* arrays ***********************************************/
$arr = Array (
' name ' = ' Gaofei ',
' word ' = ' 123456 ',
' mail ' = ' wwwwww ',
);
$arr 2=array (0=> "Tiger",1=> "Lion");
/*array_merge () merges two or more arrays (the same string key name, followed by overwriting the previous, same numeric key name, followed by no overwrite operation, but appended to the back) for the same key name only retained after one
$arr 3 = Array_merge ($arr, $arr 2);
Var_dump ($arr 3); * *

/*
* Recursive merge operations, if the array has the same string key name, these values will be merged into an array.
* If a value itself is an array, it will be merged into another array according to the corresponding key name.
* When the array has the same array key name, the latter value will not overwrite the original value, but append to the back
* $arr 3 = array_merge_recursive ($arr, $arr 2);
Var_dump ($arr 3);
Print Results

Array (5) {
["Name"]=>
string (6) "Gaofei"
["word"]=>
string (6) "123456"
["Mail"]=>
string (6) "Wwwwww"
[0]=>
string (5) "Tiger"
[1]=>
string (4) "Lion"
}*/

/*array_rand (array, parameter) randomly extracts several arrays or strings from an array
$arr 4 = Array_rand ($arr, 2);
Print_r ($arr 4);
Printing Results Verification Code making is this principle
Array
(
[0] = = Name
[1] = = Mail
)*/

/ * Scrambled Array order * /
/*shuffle ($arr);
Print_r ($arr);
Print Results
Array
(
[0] = Gaofei
[1] = 123456
[2] = Wwwwww
)

*/





































Simple examples of common arrays

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.