Example analysis of PHP array operation

Source: Internet
Author: User
Tags array length

The example of this article analyzes the PHP array operation. Share to everyone for your reference, specific as follows:

An array of PHP is an important part of it. Examples of actions are as follows:

<?php
FUNCTIONBR () {
Echo ' <br/>===============================================<br/> ';
}
$arr 1=array ();
$arr 1[] = ' x ';
$arr 1[] = ' a ';
$arr 1[] = ' e ';
$arr 1[] = ' C ';
$arr 1[] = ' h ';
Adding an array
Array_push ($arr 1, 3, 23, 55);
Array length
Echo ' The size of array is: '. Count ($arr 1). <br/> ';
Reverse
Var_dump (Array_reverse ($arr 1));
Sort-Direct action on an array
Sort ($arr 1);
Var_dump ($arr 1);
Sort-Sort by string
Sort ($arr 1, sort_string);
Var_dump ($arr 1);
Range
$arr 2= Range (' A ', ' H ');
Connection
$arrTemp 1= implode ('-', $arr 2);
ECHO$ARRTEMP1;
echo ' <br/> ';
Cutting
Echo ' ['. Implode ('], array_reverse (Explode ('-', $arrTemp 1)). '
Array merging, which will rearrange the indexes
$arr 3=array_merge ($arr 1, $arr 2);
Var_dump ($arr 3);
To delete an array element
Array_shift ($arr 3);
Array_pop ($arr 3);
Unset ($arr 3[4]);
Array_splice ($arr 3, 6, 2);
Var_dump ($arr 3);
Extract array, the original array is unchanged
$arr 4=array_slice ($arr 3, 2,3);
Var_dump ($arr 4);
Associative arrays
$fruits =array (' Red ' => ' apple ', ' yellow ' => ' banana ', ' green ' => ' lime ');
Array keys
$colors =array_keys ($fruits);
Array value
$fla =array_values ($fruits);
Var_dump ($colors);
Var_dump ($FLA);
Find
Echoin_array (' green ', $colors);
echo ' <br/> ';
Echoin_array (' black ', $colors)? ' In ': ' Isn't in ';
echo ' <br/> ';
Echoarray_key_exists (' Yellow ', $fruits);
echo ' <br/> ';
Sort keys
Ksort ($fruits);
Var_dump ($fruits);
Sort by value
Asort ($fruits);
Var_dump ($fruits);
Cycle
foreach ($fruitsas $key=> $value) {
Echo$key ' => '. $value. ' <br/> ';
}
echo ' <br/> ';
$f = $fruits;
while ($elem = each ($f)) {
echo$elem[' key '. '-'. $elem [' value ']. ' <br/> ';
}
echo ' <br/> ';
$arr 5=array (2, 8, 100, 33,-18);
Find the maximum minimum value
Echomax ($arr 5);
echo ' <br/> ';
Echomin ($arr 5);
echo ' <br/> ';
Echoarray_sum ($arr 5);
echo ' <br/> ';
Functiondouble ($x) {
Echo ($x * 2). ' ';
}
Array element Application function
Array_walk ($arr 5, ' double ');
Functioncheck ($x) {
Return$x> 20;
}
Screening
Var_dump (Array_filter ($arr 5, ' check '));
$arr 6= Range (1,10);
Echo ' Random number: '. Array_rand ($arr 6);
Statistics
Count (); sizeof (); Array_count_values ();
$arr 7=array (4,5,1,2,3,1,2,1);
$ac =array_count_values ($arr 7);
Count the occurrences of each value
Var_dump ($AC);
$arr 8=array (' key1 ' => ' v1 ', ' key2 ' => ' v2 ', ' Key3 ' => ' v3 ');
Extract ($arr 8);
echo "$key 1 $key 2 $key 3";
Fill
$input =array (12,10,9);
Var_dump (Array_pad ($input, 5, 0));
Var_dump (Array_pad ($input,-5, 0));
?>

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.