PHP array merging, inversion, de-weight, adding elements, etc.;

Source: Internet
Author: User

Parameter Default value
Function ABC ($a, $b, $c = 0) {
echo $a, $b, $c;
//}
ABC (1,3);
The result equals 130;

echo "<br>";

A vertical look at the code;
echo "<pre>";
function Def () {
$arr = Func_get_args ();
Var_dump ($arr);
//}
def (1,2,3,4,5);

Array definition
Two methods of definition
$arr 1 = [];
$arr 2 = array ();

Indexed array-
$arr 1 = [All-in-a-box, "ABC", and ";
Associative array, the last one without commas. One is key,1 is value;
$arr 2 = [
"One" = 1,
"Both" = 2,
"Three" = 3
//];
echo $arr 2["one"];

Array method
Bounce something out.
Array_pop ($arr 2);

add element, return index value
Array_push ($arr 2, "XYZ");


echo "<pre>";
Var_dump ($arr 2);
echo "<pre>";
Length
Count ($arr 2);
echo Count ($arr 2);

Determine if an array has a value
In_array ("1", $arr 2);
Var_dump (In_array ("1111", $arr 2));

Merging arrays
$arr 3 = Array_merge ($arr 1, $arr 2);
Var_dump ($arr 3);

Array inversion
$arr 4 = Array_reverse ($arr 2);
Var_dump ($arr 4);

Array de-weight
$arr 5 = Array_unique ($arr 1);
Var_dump ($arr 5);

To delete an element in an array
Unset ($arr 1[0]);
Var_dump ($arr 1);


Iterating through an array

$arr 1= [1,2,3,4,5,6,7];
$arr 2= [
"One"=1,
"Both"=2,
"Three"=3,
"Four"=4
];
For loop
for ($i = 0; $i < count ($arr 1); $i + +) {
Var_dump ($arr 1[$i]);
//}

The foreach traversal, where value is named, can be changed.
foreach ($arr 2 as $value) {
Var_dump ($value);
//}
Here the key and value are named, can be changed, and the corresponding changes can be made below.
foreach ($arr 2 as $key = = $value) {
Echo $key;
echo "-----";
Echo $value;
echo "<br>";
//}

Pointer traversal
echo "<pre>";
//var_dump (each ($arr 2));

PHP array merging, inversion, de-weight, adding elements, etc.;

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.