PHP program problems: Summary and description of array instances. Today, I suddenly encountered a php problem. I found this article very old, but very practical, in the next article, I will sort out more than 30 new array-related letters in the updated php5 array PHP4.0. today I suddenly encountered a php problem. I found this article very old, but it is very practical. next I will sort out the updated php5 array.
PHP4.0 contains more than 30 new array-related functions. Many common functions allow you to check for specific objects in a given array, count array elements, add or delete elements, or sort elements.
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 ".
Reference content is as follows: $ NamesArray = array ("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John "); $ LookingFor = "Albert "; If (in_array ($ lookingFor, $ namesArray )){ Echo "You 've got it! "; } Else { Echo "Not found in this array! "; } ?> |
If You change the value of $ lookingFor to "Mary", You will get the message "You 've got it !" -- Because "Mary" is part of $ namesArray.
To count array elements, you can use the count () function:
Reference content is as follows: $ NamesArray = array ("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John "); $ Count = count ($ namesArray);?> 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. During merging, each array is arranged in the required order. If your array already has internal sorting, 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 ():
Reference content is as follows: /* Create the original array */ $ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear "); /* Add to the original array */ Array_push ($ fruitArray, "grape", "pineapple", "tomato "); /* List each element by its key value */ While (list ($ key, $ value) = each ($ fruitArray )){ Echo "$ key: $ value "; } ?> |
This will show:
Reference content is as follows: 0: apple 1: orange 2: banana 3: kiwi 4: pear 5: grape 6: pineapple 7: tomato |
When you need to add elements to the beginning of an array, the code is very similar. The difference lies in the function name: array_unshift () rather than array_push ().
Reference content is as follows: /* Create the original array */ $ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear "); /* Add to the original array */ Array_unshift ($ fruitArray, "grape", "pineapple", "tomato "); /* List each element by its key value */ While (list ($ key, $ value) = each ($ fruitArray )){ Echo "$ key: $ value "; } ?> |
This will show:
Reference content is as follows: 0: grape 1: pineapple 2: tomato 3: apple 4: orange 5: banana 6: kiwi 7: pear |
The array_merge () function combines two or more arrays.
Reference content is as follows: /* Create the original array */ $ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear "); $ VegArray = array ("carrot", "green beans", "asparagus", "artichoke", "corn "); /* Merge into an array */ $ GoodfoodArray = array_merge ($ fruitArray, $ vegArray ); /* List each element by its key value */ While (list ($ key, $ value) = each ($ goodfoodArray )){ Echo "$ key: $ value "; } ?> |
This will show:
Reference content is as follows: 0: apple 1: orange 2: banana 3: kiwi 4: pear 5: carrot 6: green beans 7: asparagus 8: artichoke 9: corn
|
Now that you have added and merged elements to the array, you can now delete the element function. You can use the array_pop () function to delete an element from the end of an array. If array_shift () is used, an element is deleted from the beginning of the array. In fact, when you delete an element from an array, this element is still available to you-when you pop or shift an element from an existing array.
- 2 pages in total:
- Previous Page
- 1
- 2
- Next page
PHP4.0 contains more than 30 new array functions...