Basic php Tutorial-array operations

Source: Internet
Author: User
If you have a large array and all you need to do is find an existing given value, you can use in_array () to return true or false, the following code outputs "Notfoundinthisarray" because you will find an existing "Alber" in $ namesArray"

If you have a large array and all you need to do is find an existing given value, you can use in_array () to return true or false, the following code outputs "Not found in this array" because you will find an existing "Alber" in $ namesArray ".

If you have a large array and all you need to do is find an existing given value, you can use in_array () to return true or false, the following code outputs "Not found in this array" because you will find an existing "Alber" in $ namesArray ".

  1. $ NamesArray = array ("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John ");
  2. $ LookingFor = "Albert ";
  3. If (in_array ($ lookingFor, $ namesArray )){
  4. Echo "You 've got it! ";
  5. } Else {
  6. Echo "Not found in this array! ";
  7. }
  8. ?>

If You change the value of $ lookingFor to "Mary", You will receive the message "You 've got it !", Because "Mary" is part of $ namesArray.

To count array elements, you can use the count () function:

  1. $ NamesArray = array ("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John ");
  2. $ Count = count ($ namesArray );
  3. ?>

The $ count value is 7.

You can add elements to any array. whether at the beginning or end of an existing array, you can also use a function to create a new array containing two or more array elements, when merging, each array is arranged in the required order. if your array already has an internal order, you need to re-sort the new merged array.

Let's start by adding elements to the end of an existing array and use the array_push () function ():

  1. /* Create the original array */
  2. $ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
  3. /* Add to the original array */
  4. Array_push ($ fruitArray, "grape", "pineapple", "tomato ");
  5. /* List each element by its key value */
  6. While (list ($ key, $ value) = each ($ fruitArray )){
  7. Echo "$ key: $ value
    ";
  8. }
  9. ?>

This will show:

0: apple1: orange2: banana3: kiwi4: pear5: grape6: pineapple7: tomato

When you need to add elements to the beginning of an array, the code is very similar. The difference is only the function name: array_unshift () rather than array_push ():

  1. /* Create the original array */
  2. $ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
  3. /* Add to the original array */
  4. Array_unshift ($ fruitArray, "grape", "pineapple", "tomato ");
  5. /* List each element by its key value */
  6. While (list ($ key, $ value) = each ($ fruitArray )){
  7. Echo "$ key: $ value
    ";
  8. }
  9. ?>
  10. // This will display:
  11. // 0: grape
  12. // 1: pineapple
  13. // 2: tomato
  14. // 3: apple
  15. // 4: orange
  16. // 5: banana
  17. // 6: kiwi
  18. // 7: pear

The array_merge () function combines two or more arrays:

  1. /* Create the original array */
  2. $ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
  3. $ VegArray = array ("carrot", "green beans", "asparagus", "artichoke", "corn ");
  4. /* Merge into an array */
  5. $ GoodfoodArray = array_merge ($ fruitArray, $ vegArray );
  6. /* List each element by its key value */
  7. While (list ($ key, $ value) = each ($ goodfoodArray )){
  8. Echo "$ key: $ value
    ";
  9. }
  10. ?>
  11. // This will display:
  12. // 0: apple
  13. // 1: orange
  14. // 2: banana
  15. // 3: kiwi
  16. // 4: pear
  17. // 5: carrot
  18. // 6: green beans
  19. // 7: asparagus
  20. // 8: artichoke
  21. // 9: corn

Now you have added and merged elements to the array. now you can use the array_pop () function to delete an element from the end of the array. if you use the array_shift () function (), delete an element from the beginning of the array. In fact, when you delete an element from the array, this element is still available for you-when you pop or shift an element from an existing array.

Use the array_pop () function to delete a value from the end of the array:

  1. /* Create an array */
  2. $ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
  3. /* A value is displayed at the end */
  4. $ Popped = array_pop ($ fruitArray );
  5. /* List the content of the new array and the pop-up values */
  6. While (list ($ key, $ value) = each ($ fruitArray )){
  7. Echo "$ key: $ value
    ";
  8. }
  9. Echo"
    And finally, in $ popped: $ popped ";
  10. ?>
  11. // This will display:
  12. // 0: apple
  13. // 1: orange
  14. // 2: banana
  15. // 3: kiwi

Next, delete a value from the end of the array:

  1. /* Create an array */
  2. $ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
  3. /* Remove a value from the array header */
  4. $ Shifted = array_shift ($ fruitArray );
  5. /* List the content of the new array and the removed value */
  6. While (list ($ key, $ value) = each ($ fruitArray )){
  7. Echo "$ key: $ value
    ";
  8. }
  9. Echo"
    And finally, in $ shifted: $ shifted ";
  10. ?>
  11. // This will display:
  12. // 0: orange
  13. // 1: banana
  14. // 2: kiwi
  15. // 3: pear
  16. // And finally, in $ shifted: apple

There are many functions that can help you sort array elements, but I will demonstrate basic sorting to help you understand the process:

  1. /* Create the original array */
  2. $ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
  3. /* Sort */
  4. Sort ($ fruitArray );
  5. /* Reset it to display the array correctly from start to end */
  6. /* List each element by its key value */
  7. While (list ($ key, $ value) = each ($ fruitArray )){
  8. Echo "$ key: $ value
    ";
  9. }
  10. ?>
  11. // This will display:
  12. // 0: apple
  13. // 1: banana
  14. // 2: kiwi
  15. // 3: orange
  16. // 4: pear

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.