PHP Operation Array Discussion

Source: Internet
Author: User

<? PHP

// 1. Determine whether a string exists in the Set
$ Namesarrays = array ("aaaaaaa", "bbbbbbbbbb", "cccccccccc", "ddddddddd ");
$ Lookingfor = "aaaaaaa ";
If (in_array ($ lookingfor, $ namesarrays ))
{
Echo "content! ";

} Else
{
Echo "no data ";
}
 
// 2. determine the number of elements in the array
$ COUNT = count ($ namesarrays );
Echo "number of sets:". $ count. "<br/> ";

// 3. add an element to the set. By default, the element is added to the end of the set
echo " 3. add elements to a collection
";
array_push ($ namesarrays," longjie ");
while (List ($ key, $ value) = each ($ namesarrays)
{< br> echo $ key. ":". $ value. "
";
}< br>
echo " 4. add elements at the beginning of the array
";
// 4. add an element at the beginning of the array
array_unshift ($ namesarrays, "aaaaaaaaaaaaaaa");
while (List ($ K, $ v) = each ($ namesarrays ))
{< br> echo $ K. ":". $ v. "
";
}

echo " 5. merge two arrays
";
$ array1 = array (" red "," blue "," yellow ");
$ array2 = array ("10001", "10002", "10003");
$ mergearray = array_merge ($ array1, $ array2 );
while (List ($ key, $ Val) = each ($ mergearray)
{< br> echo $ key. ":". $ Val. "
";
}< br>
echo " 6. delete the last element in the array
";
$ arraypops = array (" 1001 "," 1002 "," 1003 "," 1004 ", "1005");
$ arraypoped = array_pop ($ arraypops);
while (List ($ key, $ Val) = each ($ arraypops ))
{< br> echo $ key. ":". $ Val. "
";
}< br> echo "deleted element: $ arraypoped
";

echo " 6. delete the first element in the array
";
$ arrayshifts = array (" Zhang San "," Li Si "," Wang Wu ");
echo " *****************/
";
while (List ($ key, $ Val) = each ($ arrayshifts)
{< br> echo $ key. ":". $ Val. "
";
}< br> $ arrayshifted = array_shift ($ arrayshifts);
echo " *****************/
";
while (List ($ key, $ Val) = each ($ arrayshifts)
{< br> echo "delete :". $ key. ":". $ Val. "
";
}< br> echo "deleted element $ arrayshifted
";
echo " 7. array sorting
";

$ Fruitarray = array ("apple", "orange", "banana", "Kiwi", "Pear ");
/* Sort the array */
Sort ($ fruitarray );
/* Display each element and its serial number */
While (List ($ key, $ value) = each ($ fruitarray ))
{
Echo "$ key: $ value <br> ";
}
/***** Sort *******/
$ A = array ("apple", "orange", "banana", "Kiwi", "Pear ");
// Sort
Sort ($ );
/* Sort the array */
Foreach ($ A as $ K)
{
// Echo $ A [$ K]. "=>". $ v. "<br/> ";
Echo $ K. "<br/> ";
}

?>

Run the copy operation directly.

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.