PHP array tutorial

Source: Internet
Author: User
Tags php array tutorial
PHP array tutorial definition array

The PHP array is a group of ordered variables, each of which is called an element.

1. define an array
You can use the array () language structure to create an array. It accepts a certain number of key => value parameter pairs separated by commas.
Array ([key =>] value,...) // key can be a number or string // value can be any value
Example 1:

$ Phpjc = array (
0 => 'word ',
3 => 'Excel ',
'Outlook ',
'Access ');
Print_r ($ phpjc );
?>

The output result is as follows:

Array ([0] => word [3] => excel [4] => outlook [5] => access)

Example 1 defines an array named phpjc. The value of the first element is word (note: The array is counted from 0), and the second element is empty, the third element is excel. The fourth and fifth elements are automatically generated later.
You can create an empty array by assigning an array () without parameters to the variable, and then add the value by using the square brackets [] syntax. (Note: You can also use the array_push () function to add a value to the array !!!)
Example 2:

$ Phpjc = array ();
$ Phpjc [] = "one ";
$ Phpjc [] = "two ";
Echo $ phpjc [0]."
";
Echo $ phpjc [1];
?>

The output result is as follows:

One
Two


II. reading array elements
Use string indexes (or keys) to access the values stored in the array
Example 3:

$ Phpjc = array ("first" => 1, "second" => 2, "third" => 3 );
Echo $ phpjc ["second"];
$ Phpjc ["third"] = 5; // change the value of the third element from "3" to "5"
Echo $ phpjc ["third"];
?>


PHP has many array-related functions. the following examples are used to describe is_array (), n_array (), count (), array_push (), array_unshift (), array_merge (), and array_pop (), array_shift (), sort ()

1. is_array () function

Array or not
-----------------------------------------------------------
2. in_array () function
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 ".

Code

$ 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! ";
}
?>

-----------------------------------------------------------

3. count () function
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:

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

The $ count value is 7.


-----------------------------------------------------------

4. array_push () function
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 ():

Code

/* 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:

0: apple
1: orange
2: banana
3: kiwi
4: pear
5: grape
6: pineapple
7: tomato

-----------------------------------------------------------
5. array_unshift () function
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 ().

Code

/* 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:

0: grape
1: pineapple
2: tomato
3: apple
4: orange
5: banana
6: kiwi
7: pear

-----------------------------------------------------------
6. array_merge () function
The array_merge () function combines two or more arrays.

Code

/* Create the original array */
$ FruitArray = array ("apple", "orange", "banana", "kiwi", "pear ");
/* Create the second array */
$ 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:

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.

-----------------------------------------------------------

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

Code

/* 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
";
}
Echo"
And finally, in $ popped: $ popped ";
?>

This will show:

0: apple
1: orange
2: banana
3: kiwi
And finally, in $ popped: pear


-----------------------------------------------------------
8. array_shift () function
Next, delete a value from the end of the array:

Code

/* 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
";
}
Echo"
And finally, in $ shifted: $ shifted ";
?>

This will show:

0: orange
1: banana
2: kiwi
3: pear and finally, in $ shifted: apple

-----------------------------------------------------------
9. sort () function
There are many functions that can help you sort array elements. However, I will demonstrate basic sorting to help you understand the process:

Code

/* 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
";
}
?>

This will show:

0: apple
1: banana
2: kiwi
3: orange
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.