Some basic uses of common arrays in PHP

Source: Internet
Author: User

Some basic uses and operations of common arrays in PHP:

Indexed array
$arr =array ("AA", "SS", "DD", "FF");
echo $arr [0]. $arr [1]. $arr [2]. ', '. $arr [3];

Gets the length of the array
echo count ($arr);

Iterating through an indexed array
for ($i =0; $i <count ($arr); $i + +)
{
echo $arr [$i];
echo "\ n";
}

foreach ($arr as $item)
{
Echo $item;
echo "\ n";
}

Associative arrays
$arr =array ("a" = "a", "s" = "s", "q" = "q");
foreach ($arr as $key = $item)
{
echo $key. ' = '. $item;
echo "\ n";
}

Array sorting
Sort in ascending majority group
$arr =array ("6", "2", "1", "7", "5");
Sort ($arr);
foreach ($arr as $item)
{
Echo $item;
echo "\ n";
}
Sort in descending order
Rsort ($arr);
foreach ($arr as $item)
{
Echo $item;
echo "\ n";
}

Sort associative arrays in ascending order based on values
$age =array ("A" and "one"), "s" and "9", "R" and "All", "D" and "8";
Asort ($age);
foreach ($age as $key = $item)
{
echo $key. ' = '. $item;
echo "\ n";
}
Sort associative arrays in descending order by value
$age =array ("A" and "one"), "s" and "9", "R" and "All", "D" and "8";
Arsort ($age);
foreach ($age as $key = $item)
{
echo $key. ' = '. $item;
echo "\ n";
}
Sort associative arrays in ascending order by key
$age =array ("A" and "one"), "s" and "9", "R" and "All", "D" and "8";
Ksort ($age);
foreach ($age as $key = $item)
{
echo $key. ' = '. $item;
echo "\ n";
}
Sort associative arrays in descending order by key
$age =array ("A" and "one"), "s" and "9", "R" and "All", "D" and "8";
Krsort ($age);
foreach ($age as $key = $item)
{
echo $key. ' = '. $item;
echo "\ n";
}

Get all the values in the associative array
$age =array ("A" and "one"), "s" and "9", "R" and "All", "D" and "8";
$arr =array_values ($age);
foreach ($arr as $item)
{
Echo $item;
echo "\ n";
}
Gets all the key names in the associative array
$age =array ("A" and "one"), "s" and "9", "R" and "All", "D" and "8";
$arr =array_keys ($age);
foreach ($arr as $item)
{
Echo $item;
echo "\ n";
}

Checks if a value exists in the array
$age =array ("A" and "one"), "s" and "9", "R" and "All", "D" and "8";
Echo In_array (' 9 ', $age);

Exchanging keys and values in an array
$age =array ("A" and "one"), "s" and "9", "R" and "All", "D" and "8";
$newage =array_flip ($age);
foreach ($newage as $key = $item)
{
echo $key. ' = '. $item;
echo "\ n";
}

Returns an array of the opposite of a cell array
$age =array ("A" and "one"), "s" and "9", "R" and "All", "D" and "8";
$newage =array_reverse ($age);
foreach ($newage as $key = $item)
{
echo $key. ' = '. $item;
echo "\ n";
}

Count the occurrences of all the values in the array
$age =array ("a" = "one", "s" = "9", "r" = "9", "D" and "8");
$num =array_count_values ($age);
foreach ($num as $item)
{
echo $item. ' ‘;
}

Removes duplicate values from an array, leaving only one
$age =array ("a" = "one", "s" = "9", "r" = "9", "D" and "8");
$newage =array_unique ($age);
foreach ($newage as $key = $item)
{
echo $key. ' = '. $item;
echo "\ n";
}

Merging arrays
$arr 1=array ("1", "2", "3", "4");
$arr 2=array ("2", "3", "5", "1");
$arr =array_merge ($arr 1, $arr 2);
foreach ($arr as $item)
{
echo $item. ', ';
}

Computes the intersection of an array
$arr 1=array ("1", "2", "3", "4");
$arr 2=array ("2", "3", "5", "1");
$arr =array_intersect ($arr 1, $arr 2);
foreach ($arr as $item)
{
echo $item. ', ';
}

Calculates the difference of an array
$arr 1=array ("1", "2", "3", "4");
$arr 2=array ("2", "3", "5", "1");
$arr =array_diff ($arr 2, $arr 1);
foreach ($arr as $item)
{
echo $item. ', ';
}

Randomly fetching one or more elements from an array
$arr =array ("1", "A", "3", "E", "E", "4", "E");
$newarr =array_rand ($arr);
Echo $newarr;
echo "\ n";
$arr 1=array_rand ($arr, 3);
foreach ($arr 1 as $item)
{
echo $item. ', ';
}

To disrupt an array
$arr =array ("1", "2", "3", "4", "5", "6", "7");
Shuffle ($arr);
Print_r ($arr);
foreach ($arr as $item)
{
echo $item. ', ';
}

Rounding, removing the value after the decimal point
echo Floor (15.4);

Some basic uses of common arrays in PHP

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.