Basic php Tutorial-array operations

Source: Internet
Author: User
Tags pear php tutorial

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 ".
<? Php Tutorial
$ 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 receive the message "You 've got it !", Because "Mary" is part of $ namesArray.
To count array elements, you can use the count () function:
<? Php
$ 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, 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 ():
<? Php
/* 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 <br> ";
}
?>

 

This will show:
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 is only the function name: array_unshift () rather than array_push ():
<? Php
/* 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 <br> ";
}
?>

 

This will show:
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:
<? Php
/* Create the original array */
$ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
<? /* Create the second array */
$ VegArray = array ("carrot", "green beans", "asp tutorial aragus", "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 <br> ";
}
?>

 

This will show:
0: apple
1: orange
2: banana
3: kiwi
4: pear
5: carrot
6: green beans
7: asparagus
8: artichoke
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:
<? Php
/* Create an array */
$ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
/* A value is displayed at the end */
$ Popped = array_pop ($ fruitArray );
/* List the content of the new array and the pop-up values */
While (list ($ key, $ value) = each ($ fruitArray )){
Echo "$ key: $ value <br> ";
}
Echo "<br> and finally, in $ popped: $ popped ";
?>

 

This will show:
0: apple
1: orange
2: banana
3: kiwi
And finally, in $ popped: pear
Next, delete an element from the end of an array :???????????

 

Next, delete a value from the end of the array:
<? Php
/* Create an array */
$ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
/* Remove a value from the array header */
$ Shifted = array_shift ($ fruitArray );
/* List the content of the new array and the removed value */
While (list ($ key, $ value) = each ($ fruitArray )){
Echo "$ key: $ value <br> ";
}
Echo "<br> and finally, in $ shifted: $ shifted ";
?>

 

This will show:
0: orange
1: banana
2: kiwi
3: pear
And finally, in $ shifted: apple

 

There are many functions that can help you sort array elements. However, I will demonstrate basic sorting to help you understand the process:
<? Php
/* Create the original array */
$ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
/* Sort */
Sort ($ fruitArray );
/* Reset it to display the array correctly from start to end */
/* List each element by its key value */
While (list ($ key, $ value) = each ($ fruitArray )){
Echo "$ key: $ value <br> ";
}
?>

 

This will show:
0: apple
1: banana
2: kiwi
3: orange
4: pear

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.