Chủ Nhật, 16 tháng 2, 2014

Tài liệu PHP and MySQL by Example- P7 doc

3 $popped = array_pop($names); // Remove last element
echo "<br />After pop(): ";
4 foreach($names as $val){
echo "<em>$val </em>";
}
echo "<p>$popped was removed from the end of the
array.</p>";
?>
</pre>
</body>
</html>
Explanation
1
The!numeric!array!$names!is!created!and!assigned!a!list!of!values.
2
The!foreach!loop!is!used!to!iterate!through!the!array!and!display!its!values.
3
The!array_pop()!function!removes!the!last!element!of!the!array,!"Jerry".!The!popped!
off!value!is!returned!and!assigned!to!a!variable,!called!$popped.
4
The!$names!array!is!displayed!after!the!last!element!was!removed!with!the!
array_pop()!function.!See!Figure!8.46.
!
Figure 8.46. The last element from an array is removed with pop(). Output from Example 8.38.


Removing the First Element of an Array
The array_shift() function removes the first element from an array and returns it, decreasing the size of the array
by one element. All numerical array keys start at zero and literal keys will not be touched. If an array is empty (or is not
an array), NULL will be returned. See Example 8.39.
Format
mixed array_shift ( array &array )
!
Example:
$colors=array("red", "blue","green", "yellow"); // First element, "red", removed
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
and assigned to $shifted_off_color $shifted_off_color = array_shift( $colors);
Example 8.39.
<html><head><title>array_shift()</title></head>
<body bgcolor="lightblue">
<font face="verdana" size="+1">
<?php
echo "Before the shift: ";
1 $names=array("Tom", "Dan", "Steve", "Christian", "Jerry");
2 foreach($names as $val){
echo "<em>$val </em>";
}
3 $shifted=array_shift($names); // Remove first element
echo "<br />After the shift: ";
4 foreach($names as $val){
echo "<em>$val </em>";
}
5 echo "<p>$shifted was removed.</p>";
?>
</pre>
</body>
</html>
Explanation
1
A!numeric!array!called!$names!is!defined.
2
The!foreach!loop!is!used!to!iterate!through!the!array!and!get!the!individual!
values,!each!one!in!turn,!assigned!to!$val.
3
The!array_shift()!function!removes!and!returns!the!first!element!of!the!
array,!"Tom",!assigned!to!$shifted.
4
The!foreach!loop!is!used!again!to!iterate!through!the!array!showing!that!the!
array!has!been!shortened!by!one!element.!See!Figure!8.47.
5
The!value!returned!from!the!array_shift()!function!is!"Tom",!the!first!
element!in!the!array.!This!value!is!printed.!See!Figure!8.47.
Figure 8.47. Removing the first element of an array with shift(). Output from Example 8.39.

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Removing Duplicate Values from an Array
The array_unique() function removes duplicate values from an array and returns a new array without the
duplicates. The array_unique() function first sorts the values treated as strings, then keeps the first key
encountered for every value, and thereafter, ignores any duplicate keys. Two elements are considered equal only if they
are identical (same data type, same value); that is, the === operator applies. See Example 8.40.
Format
array array_unique ( array array )
!
Example:
unique_values = array_unique(array("apples", "pears",
"apples", "Apples")); // Removes duplicates and returns an array with unique
values.
Example 8.40.
<html><head><title>array_unique()</title></head>
<body bgcolor="cccc99">
<font face="verdana" size="+1">
<?php
echo "Before: ";
1 $numbers=array(1, 3, 5, 7, 7, 7, 9, 9, 8);
2 foreach($numbers as $val){
echo "<em>$val </em>";
}
echo "<br />After: ";
3 $numbers=array_unique($numbers); // Remove duplicates
echo '$numbers=<b>array_unique($numbers)i</b><br />';
foreach($numbers as $val){
echo "<em>$val </em>";
}
?>
</pre>
</body>
</html>
Explanation
1
A!numerically!indexed!array!called!$numbers!is!assigned!a!list!of!integers.
2
The!foreach!loop!is!used!to!loop!through!the!array,!$numbers.!The!value!of!each!of!the!
elements!is!printed.
3
Notice!that!there!are!a!number!of!duplicate!values!in!the!array!$numbers.!The!
array_unique!function!returns!an!array!with!duplicates!removed;!that!is,!an!array!of!
unique!values.!See!Figure!8.48.
!







Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 8.48. The array_unique() function. Output from Example 8.40.
!
8.2.2. Adding Elements to an Array
PHP provides built-in functions to increase the size of an array by allowing you to add elements.(see Table 8.9).
Table 8.9. Array Functions to Add Elements to an Array
Function
What+It+Does
array_push()
Pushes!a!new!element(s)!onto!the!end!of!the!array
array_splice()
Removes!and/or!adds!elements!to!an!array!at!any!position
array_unshift()
Adds!a!new!element(s)!to!the!beginning!of!the!array
!
Adding Elements to the Beginning of an Array
The array_unshift() function prepends one or more elements onto the beginning of an array, increasing the size
of the array by the number of elements that were added. It returns the number of elements that were added. See
Example 8.41.
Format
int array_unshift ( array &array, mixed var [, mixed ] )
!
Example:
$colors=("yellow", "blue", "white");
$added=array_unshift($colors,"red","green"); // "red", "green", "yellow",
"blue", "white"

Example 8.41.
<html><head><title>array_unshift()</title></head>
<body bgcolor="yellow">
<font face="verdana" size="+1">
<?php
echo "Before unshift(): ";
1 $names=array("Tom", "Dan", "Steve", "Christian", "Jerry");
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
2 foreach($names as $val){
echo "<em>$val </em>";
}
// Add new element to the beginning
3 array_unshift($names, "Willie", "Liz");
echo "<br />After unshift(): ";
foreach($names as $val){
echo "<em>$val </em>";
}
4 echo "<p>Willie and Liz were added to the beginning of the
array.</p>";
?>
</pre>
</body>
</html>




Explanation
1
The!numeric!array!called!$names!is!assigned!five!string!values.
2
The!foreach!loop!is!used!to!iterate!through!the!array!$names.!Each!value!is!printed!as!
the!loop!cycles!through!the!array.
3
The!array_unshift()!function!is!used!to!append!new!elements!to!the!beginning!of!an!
array.!In!this!example,!"Willie"!and!"Liz"!are!prepended!to!the!array!$names.
"Willie"!will!be!assigned!the!index!of!0,!and!all!the!rest!of!the!index!values!will!be!
incremented!accordingly.
4
Figure!8.49!displays!the!array!after!"Willie"!and!"Liz"!are!prepended!with!the!
array_unshift()!function.
!
Figure 8.49. Adding elements to the beginning of an array with unshift(). Output from Example 8.41.
!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Adding Elements to the End of an Array
The array_push() function pushes one or more elements onto the end of array, increasing the size of the array by
the number of elements that were added. It returns the number of elements that were added. See Example 8.42.
Format
int array_push ( array &array, mixed var [, mixed ] )
!
Example:
$colors=("yellow", "blue", "white"); $added = array_push($colors, "red",
"green"); // "yellow", "blue", "white", "red", "green"
Example 8.42.
<html><head><title>array push()</title></head>
<body bgcolor="lightblue">
<font face="verdana" size="+1">
<?php
echo "Before push(): ";
1 $names=array("Tom", "Dan", "Christian", "Jerry");
2 foreach($names as $val){
echo "<em>$val </em>";
}
3 array_push($names, "Tina", "Donna");// Add two elements
echo "<br />After push(): ";
foreach($names as $val){
echo "<em>$val </em>";
}
4 echo "<p>Tina and Donna were added to the end of the
array.</p>";
?>
</pre>
</body>
</html>
Explanation
1
The!numeric!array!called!$names!is!assigned!four!string!values.
2
The!foreach!loop!is!used!to!iterate!through!the!array!$names.!Each!value!is!printed!as!
the!loop!cycles!through!the!array.
3
The!array_push()!function!is!used!to!append!new!elements!to!the!end!of!an!array.!In!
this!example,!"Tina"!and!"Donna"!are!appended!to!the!array!$names. "Tom"!will!be!
assigned!the!index!of!0,!and!all!the!rest!of!the!index!values!are!incremented!accordingly.
4
Figure!8.50!displays!the!array!after!"Tina"!and!"Donna"!are!appended!to!it!with!the!
array_push()!function.
!





Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 8.50. The array_push() function. Output from Example 8.42.
!



Splicing an Array—Removing and Adding Elements
The word splice is often associated with splicing two pieces of rope, film, or DNA strands. It means to join. Array
elements can be removed and then what is left can be joined back together. The array_splice() function removes
a portion of the array and joins it back together, possibly replacing the removed values with new ones. The first
argument is the array, and the second argument is the place in the array (called the offset) where you want the function
to start removing elements. Offset 0 is the first position in the array. If you are taking elements from the end of the
array, the offset starts at a negative number. An offset of –1 indicates the end of the array. The third, optional argument
tells the function how many elements you want to remove. If you do not specify a length, splice() removes
everything from the offset to the end of the array. A fourth optional argument allows you to list the replacement values.
(Use the built-in count() function to get the length of the array.) The returned values are the elements that were
removed.
Simply put, splice() removes any number of elements from an array, starting at some position, and lets you replace
those elements with new ones if you want to.
The next set of examples demonstrate how array_splice() is used to remove and replace elements in an array.
Figure 8.51 displays the resulting spliced array.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 8.51. The splice() function. Each numbered example is applied to the original array.
!
!
Format
array array_splice ( array &input, int offset [, int length [,
array replacement]] )
!
Examples:
1. $foods=array("bread","milk", "eggs", "fruit", "meat");
array_splice($foods, 4); // Removes "meat" 2. $foods=array("bread","milk",
"eggs", "fruit", "meat"); array_splice($foods,0,3); // Removes "bread",
"milk", and "eggs" 3. $foods=array("bread","milk", "eggs", "fruit", "meat");
array_splice($foods, -2); // Removes "fruit" and "meat" 4.
$foods=array("bread","milk", "eggs", "fruit", "meat"); array_splice($foods,
0,0, array("fish", "cheese")); // Prepends the array with "fish" and
"cheese" 5. $foods=array("bread","milk", "eggs", "fruit", "meat");
array_splice($foods, -3, 2, "veggies"); // Backs up three from the end,
removes "eggs" and "fruit", // and replaces them with "veggies" 6.
$foods=array("bread","milk", "eggs", "fruit", "meat"); array_splice($foods,2
,count($foods),array("beer","peanuts")); // Removes "eggs", "fruit", and
"meat" and replaces them with // "beer" and "peanuts"


Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

8.2.3. Copying Elements of an Array
The array_slice() Function
In case you get the terms splice and slice confused, think of a splice as joining two pieces of tape or rope together and
think of slice as in a slice of bread or a slice of apple pie. The array_slice() function extracts a slice (some
specified elements) of an array, specified by an offset and the number of elements to extract. It returns the specified
sequence of elements from the array and resets the key/index values. If the boolean argument is set to TRUE, then the
index values will not be adjusted.
To simplify, the array_slice() function copies elements from one array, and assigns them to another array. The
array that is being sliced is not changed. See Example 8.43
Format
array array_slice ( array array, int offset [, int length [,
bool preserve_keys]] ) array = array_slice ( array_name, integer offset); array
= array_slice ( array_name, integer offset, length); array = array_slice (
array_name, integer offset, length, boolean value);
!
Example:
foods = array("bread", "milk", "eggs", "fruit"); $slice = array_slice ( $foods,
2); // $slice contains "eggs" $slice = array_slice ( $foods, 0, 2); // $slice
contains "bread" and "milk" $slice = array_slice ( $foods, -2, 1); // $slice
contains "milk" $slice = array_slice ( $foods, 0, 3, TRUE); // $slice contains
"bread", "milk", and "eggs"; the order of the // keys/index values are preserved

Example 8.43.
Code!View:!
<html><head><title>array_slice()</title></head>
<body bgcolor="cccc99">
<font face="verdana">
<pre>
<b>
<?php
1 $names=array("Tom", "Dan", "Steve", "Christian", "Jerry");
echo "Original array before slice(): <br />";
print_r($names);
2 $good_guys=array_slice($names, 0, 3);
echo "<br />New array from array_slice(0,3):<br /> ";
print_r($good_guys);
3 $chosen_ones=array_slice($names, -2);
echo "New array from array_slice(-2):<br />";
print_r($chosen_ones);
echo "Original array after the slice(): <br />";
4 print_r($names);
?>
</pre>
<b>
</body>
</html>! ! ! ! !!



Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Explanation
1
$names!is!a!numeric!array!intialized!with!a!list!of!strings.
2
The!first!argument!to!the!array_slice()!function!is!the!offset!position,!where!
to!start!selecting!elements,!and!the!second!argument!is!the!length!or!number!
of!elements!to!copy.!In!this!example,!"Tom", "Dan",!and!"Steve"!are!copied!
into!an!array!called!$good_guys.
3
In!this!example,!the!offset!starts!from!the!end!of!the!array.!An!offset!of!–2!
means!back!up!two!positions!from!the!end!of!the!array.!Because!a!length!is!not!
specified,!the!array_splice()!function!will!copy!the!last!two!elements,!
"Christian"!and!"Jerry",!from!the!array!and!assign!them!to!$good_guys.
4
For!output!of!this!example,!see!Figure!8.52.
Figure 8.52. The array_slice() function. Output from Example 8.43.

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

Không có nhận xét nào:

Đăng nhận xét