A tutorial on PHP array array _php tips

Source: Internet
Author: User
Tags pear
Defining arrays
An array of arrays is an ordered set of variables in which each variable is called an element.
One, defining an array
You can use the array () language structure to create a new array. It accepts a certain number of key => value parameter pairs separated by commas.
Array ([key =>] value, ...)//key can be a number or the string//value can be any value
Example 1:
Copy Code code as follows:

<?php
$PHPJC = Array (
=> ' word ',
=> ' Excel ',
' Outlook ',
' Access ');
Print_r ($PHPJC);
?>

The output results are as follows:
Copy Code code 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), the second element is empty, and the third element is: Excel, which automatically generates the fourth and fifth elements after

You can create an empty array by giving the variable an array () with no parameters, and then you can add the value by using the square brackets [] syntax. (Note: Alternatively, you can add a value to the array using the Array_push () function!!! )
Example 2:
Copy Code code as follows:

<?php
$PHPJC = Array ();
$PHPJC [] = "one";
$PHPJC [] = "two";
echo $PHPJC [0]. " <br> ";
echo $PHPJC [1];
?>

The output results are as follows:
Copy Code code as follows:

One
Two

Reading the elements of an array
Use a string index (or key) to access values stored in an array
Example 3:
Copy Code code as follows:

<?php
$PHPJC = Array ("=>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"];
?>

There are many array-related functions in PHP that illustrate Is_array (), N_array (), count (), Array_push (), Array_unshift (), Array_merge (), Array_pop (), Array_shift (), sort ()
1.is_array () function
is an array
-----------------------------------------------------------
2.in_array () function
If you have a large array, and you want to do only to find a given value that exists, you can use In_array () to return True or false. The following code prints "not found in"-because you will be looking for a "alber" that does not exist in $namesarray.
Copy Code code as follows:

<?php
$namesArray = Array ("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");
$lookingFor = "Albert";
if (In_array ($lookingFor, $namesArray)) {
echo "You ' ve found it!";
} else {
echo "Not found in this array!";
}
?>

-----------------------------------------------------------
3.count () function
If you change the value of $lookingfor and turn it into "Mary," you will get the message "you ' ve found it!" --because "Mary" is part of the $namesarray.
If you want to count the array elements, you can use the count () function:
Copy Code code as follows:

<?php
$namesArray = Array ("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");
$count = count ($namesArray);
?>

The $count value will be 7.
-----------------------------------------------------------
4.array_push () function
You can add elements to any array, either at the beginning or the end of an existing array. You can also use a function to create a new array that contains two or more arrays of elements. Each array is arranged in the order you want it to be merged. If your array already has an internal sort, you need to reorder the new merged arrays. Let's start with adding elements to the end of an existing array, using the function Array_push ():
Copy Code code as follows:

<?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 (the list ($key, $value) = each ($fruitArray)) {
echo "$key: $value <br>";
}
?>

This will show:
Copy Code code as follows:

Apple
Orange
Banana
Kiwi
Pear
Grape
Pineapple
Tomato

-----------------------------------------------------------
5.array_unshift () function
When you need to add an element to the beginning of an array, the code is very similar. The difference is just the function name: Array_unshift () instead of Array_push ().
Copy Code code as follows:

<?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 (the list ($key, $value) = each ($fruitArray)) {
echo "$key: $value <br>";
}
?>

this will show:
Copy Code code as follows:

Pineapple
Tomato
Apple
Orange
Banana
Kiwi
Pear

-----------------------------------------------------------
6.array_merge () function
The function Array_merge () merges two or more arrays.
Copy Code code as follows:

<?php
/* Create the original array * *
$fruitArray = Array ("Apple", "orange", "banana", "kiwi", "pear");
/* Create a second array */
$vegArray = Array ("carrot", "green beans", "asparagus", "artichoke", "corn");
/* Merge to an array * *
$goodfoodArray = Array_merge ($fruitArray, $vegArray);
/* List each element by its key value * *
while (the list ($key, $value) = each ($goodfoodArray)) {
echo "$key: $value <br>";
}
?>

This will show:
Copy Code code as follows:

Apple
Orange
Banana
Kiwi
Pear
Carrot
Green beans
Asparagus
Artichoke
Corn

Now that we've added elements and merges to the array, we're going to practice deleting the element functions. You can use the function Array_pop () to delete an element from the end of an array. If you use the function Array_shift (), you delete an element from the beginning of an array. And actually when you delete an element from an array, this element is still available to you-when you pop or shift elements from an existing array.
-----------------------------------------------------------
7.array_pop () function
Use the Array_pop () function to remove a value from the end of the array:
Copy Code code as follows:

<?php
/* Create an array */
$fruitArray = Array ("Apple", "orange", "banana", "kiwi", "pear");
/* Eject a value at the end/*
$popped = Array_pop ($fruitArray);
/* Lists the new array contents, and the values that pop up.
while (the list ($key, $value) = each ($fruitArray)) {
echo "$key: $value <br>";
}
echo "<br>and Finally, in $popped: $popped";
?>

This will show:
Copy Code code as follows:

Apple
Orange
Banana
Kiwi
And finally, in $popped: Pear

-----------------------------------------------------------
8.array_shift () function
Below, delete a value from the end of the array:
Copy Code code as follows:

<?php
/* Create an array */
$fruitArray = Array ("Apple", "orange", "banana", "kiwi", "pear");
/* Remove a value from the head of the array * *
$shifted = Array_shift ($fruitArray);
/* Lists the contents of the new array and the values removed.
while (the list ($key, $value) = each ($fruitArray)) {
echo "$key: $value <br>";
}
echo "<br>and Finally, in $shifted: $shifted";
?>

This will show:
Copy Code code as follows:

Orange
Banana
Kiwi
Pear and finally, in $shifted: Apple

-----------------------------------------------------------
9.sort () function
There are a number of functions that can help you sort the array elements. But I'll show you the basics of sorting to help you understand its process:
Copy Code code as follows:

<?php
/* Create the original array * *
$fruitArray = Array ("Apple", "orange", "banana", "kiwi", "pear");
/* Sort * *
Sort ($fruitArray);
/* Reset it to display the array correctly from beginning to end * *
/* List each element by its key value * *
while (the list ($key, $value) = each ($fruitArray)) {
echo "$key: $value <br>";
}
?>

This will show:
Copy Code code as follows:

Apple
Banana
Kiwi
Orange
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.