Summary and description of PHP array examples _php tips

Source: Internet
Author: User
Tags pear
You can use In_array () to return TRUE or False if you have a large array and you want to do only to find a given value that exists. The following code outputs the not found in this array 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!";
}
?>


If you change the value of $lookingfor to "Mary", you will get the message "you ' ve found it!" because "Mary" is part of $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.
You can add elements to any array, whether at the beginning or end of an existing array, you can also use functions to create a new array that contains two or more arrays of elements, each of which will be sorted in the order you want them to be, and 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:

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, except for 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:

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

0:apple
1:orange
2:banana
3:kiwi
4:pear
5:carrot
6:green Beans
7:asparagus
8:artichoke
9:corn

Now that you've added elements and merges to the array, you can now practice removing the element function, you could use the function Array_pop () to remove an element from the end of a list, and if you use the function Array_shift (), remove an element from the beginning of an array, but 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:
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:

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

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

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.