PHP Array instance illustrates _php basics

Source: Internet
Author: User
Tags pear
PHP4.0 has more than 30 new array-related functions. Many of these common functions allow you to check whether a given array has a specific object, an array element count, an addition or deletion element, or a sort of element.

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.


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

}

?>
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:


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

$count = count ($namesArray);?>
The $count value will be 7.

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 ():


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

0:apple

1:orange

2:banana

3:kiwi

4:pear

5:grape

6:pineapple

7:tomato

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 ().


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

0:grape

1:pineapple

2:tomato

3:apple

4:orange

5:banana

6:kiwi

7:pear

The function Array_merge () merges two or more arrays.


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

0:apple

1:orange

2:banana

3:kiwi

4:pear

5:carrot

6:green Beans

7:asparagus

8:artichoke

9: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.

Use the Array_pop () function to remove a value from the end of the array:


?

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

0:apple

1:orange

2:banana

3:kiwi

And finally, in $popped: Pear

Next, delete an element from the "end of" an array:?????????????????????

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


?

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

0:orange

1:banana

2:kiwi

3:pear

And finally, in $shifted: Apple

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:


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

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.