Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 50 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
50
Dung lượng
1,49 MB
Nội dung
A parse error occurred because single quotes were embedded within double quotes. This can be fixed by concatenating
the strings as follows:
echo $book['Title'] . "<br />";
!
or by surrounding the array element with curly braces:
echo "{$book['Title']}<br />"
!
Do not eliminate the quotes around the key as shown here:
echo $book[Title];
!
as PHP will treat the key Title as a constant, not as a string. You might inadvertently use a key value that has been
defined somewhere else as a constant to produce the error message “E_NOTICE (undefined constant).”
Mixing Elements in an Array
The index/key value in an array can be a positive or negative number or a string, and the value associated with it can be
a string, a number, another array, and so on. If an index value is not given, PHP provides a numeric index, incrementing
the index of the next position in the array by 1.
Example 8.7 demonstrates how the index and the value of the elements can be mixed. The output is shown in Figure
8.11.
Example 8.7.
<html>
<head><title>Using a Negative Index</title></head>
<body bgcolor="lightgreen">
<b>
<?php
1 $colors=array(-1 =>'purple','orange',"brown"=>"burnt
sienna");
2 $colors[]=255;
3 echo "\$colors[-1] is ". $colors[-1] . ".<br />";
4 echo "\$colors[0] is $colors[0].<br />";
echo "\$colors[1] is $colors[1].<br />";
5 echo "\$colors['brown'] is " . $colors['brown'] . ".<br
/>";
6 // echo "\$color['brown'] is {$colors['brown']}<br />";
?>
</b>
</body>
</html>
Explanation
"
#$%!,)),-!$colors!03,)30!2*3$!,!&%4,3*6%!*&/%:7!-18!?@?!2* !,//!"!35!%,+$!
+5&0%+'3*6%!*&/%:!*&!3$%!,)),-7!05!3$,3!%.%(%&3!'orange'!2* !;%!,005+*,3%/!
2*3$!*&/%:!<8!#$%!3$*)/!%.%(%&3!*0!,!03)*&47!"brown"7!,005+*,3%/!2*3$!"burnt
sienna"8!K1!,&53$%)!%.%(%&3!*0!,//%/!.,3%)!5&7!3$%!*&/%:!2* !;%!&'(%)*+!,&/!
*&+)%(%&3%/!;-!"!1)5(!3$%!.,03!&'(%)*+!*&/%:8
9
>!&%2!%.%(%&3!*0!,//%/!35!3$%!,)),-8!T%+,'0%!3$%!.,03!&'(%)*+!*&/%:!2,0!<7!3$%!
*&/%:!51!3$*0!%.%(%&3!2* !;%!"8
=
#$%!6,.'%!51!3$%!,)),-!%.%(%&3!,3!*&/%:!Q"!*0!%:3),+3%/!,&/!B)*&3%/8
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
A
>.3$5'4$!3$%!6,.'%!'orange'!2,0!&53!%:B.*+*3 !,00*4&%/!,&!*&/%:7!?@?!2* !
,//!"!35!3$%!B)%6*5'0!&'(%)*+!*&/%:!6,.'%8!T%+,'0%!3$,3!*&/%:!2,0!Q"7!3$%!
*&/%:!15)!'orange'!2* !;%!,00*4&%/!<8
R
@%)%!3$%!*&/%:!*0!,!03)*&47!"brown"7!,005+*,3%/!2*3$!,!6,.'%7!"burnt sienna"8!
L0*&4!,!03)*&4!2* !$,6%!&5!%11%+3!5&!3$%!&'(%)*+!*&/%:%08
V
#$*0!.*&%!*0!+5((%&3%/!5'37!;'3!B)56*/%/!35!/%(5&03),3%!3$%!'0%!51!+') !
;),+%0!2$%&!'0*&4!,)),-0!,&/!&%03%/!W'53%0U!3$,3!*07!W'53%0!2*3$*&!W'53%08!
Y$%&!,&!,)),-!%.%(%&3!*0!%&+.50%/!2*3$*&!,!03)*&4!*&!/5';.%!W'53%07!3$%!+') !
;),+%0!, 52!3$%!J%-!35!;%!0'))5'&/%/!;-!0*&4.%!W'53%07!2*3$5'3!+,'0*&4!,!?@?!
%))5)8!Y*3$5'3!3$%!+') !;),+%0!3$%!%))5)!*0I!?,)0%!%))5)I!0-&3,:!%))5)7!
'&%:B%+3%/!#ZOXG>?PO[Z>X[ZY@K#OP?>GO7!%:B%+3*&4!#ZP#\X]!5)!
#H>\K>TNO!5)#ZXLMZP#\KX]!*&GI^^2,(B^B$B_!5&!.*&%!"E
Figure 8.11. Mixed array elements. Output from Example 8.7.
!
8.1.3. Printing an Array
There are a number of ways to print the contents of an array. PHP provides built-in functions to display all of the keys
and values, or you can use loops. We discuss the built-in functions first.
The print_r() Function
The print_r() built-in function displays detailed information about a variable, such as its type, length, and contents.
It displays the value of a string, integer, or float. In the case of arrays, print_r() displays all of the elements of an
array; that is, the key–value pairs. (Use the HTML <pre> tag, to display each element on a separate line; otherwise,
the output is displayed on one line.) If you would like to capture the output of print_r() in a variable, set the return
parameter to boolean TRUE. The print_r() function can also be useful when debugging a program.
Remember that print_r() will move the internal array pointer to the end of the array. Use reset() to bring it back
to the beginning. PHP 5 appears to reset the pointer automatically after using print_r().
Example 8.8 demonstrates the print_r() function. The output is shown in Figure 8.12.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Format
bool print_r ( mixed expression [, bool return] )
!
Example:
print_r($colors); // Display the contents of the array $list=print_r( $colors,
true); // Save the return value in $list reset($colors); // Move internal array
pointer to array beginning
Example 8.8.
<html>
<head><title>The print_r() Function</title></head>
<body bgcolor="yellow">
<b>
<pre>
<?php
1 $colors=array('red','green', 'blue','yellow');
2 $book=array('Title' => 'War and Peace',
'Author' => 'Tolstoy',
'Publisher' => "Oxford University Press",
);
3 print_r($colors);
echo "<hr>";
4 print_r($book);
?>
</b>
</pre>
</body>
</html>
Explanation
"
>!&'(%)*+!,)),-!+, %/!$colors!*0!+)%,3%/!2*3$!15')!%.%(%&308
9
>&!,005+*,3*6%!,)),-!+, %/!$book!*0!+)%,3%/!2*3$!J%-Q6,.'%!B,*)08
=
#$%!print_r()!1'&+3*5&!/*0B.,-0!3$%!,)),-!*&!,!$'(,& !)%,/,;.%!15)(,37!
&'(%)*+!*&/%:%0!5&!3$%!.%13!51!3$%!=>!5B%),35)!,&/!3$%!6,.'%!51!3$%!%.%(%&30!5&!
3$%!)*4$3!0*/%8!L0%!3$%!@#MN!<pre>!3,4!35!B)%0%&3!3$%!,)),-!5&!0%B,),3%!.*&%0U!
53$%)2*0%!*3!2* !;%!/*0B.,-%/!,0!5&%!.5&4!.*&%8
A
#$%!print_r()!1'&+3*5&!/*0B.,-0!3$%!,005+*,3*6%!,)),-7!J%-Q6,.'%!B,*)0!*&!,&!
%,0-_35_)%,/!15)(,38
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 8.12. Printing an array with the print_r() function. Output from Example 8.8.
!
!
!
Example 8.9.
<html>
<head><title>The print_r() Function</title></head>
<body bgcolor="CCFF66">
<pre>
<b>
<?php
$colors=array('red','green', 'blue','yellow');
$book=array('Title' => 'War and Peace',
'Author' => 'Tolstoy',
'Publisher' => 'Oxford University Press',
);
1 $display= print_r($colors,true);// Assign output to
$display
2 echo $display;
3 reset($colors);
?>
</b>
</pre>
</body>
</html>
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Explanation
"
T-!4*6*&4!print_r()!,&!,//*3*5&,.!;55.%,&!,)4'(%&3!51!TRUE7!-5'!+,&!+,B3')%!3$%!
5'3B'3!51!print_r()!*&!,!6,)*,;.%8
9
#$%!$display!6,)*,;.%!+5&3,*&0!3$%!5'3B'3!51!3$%!print_r()!1'&+3*5&U!3$,3!*07!3$%!
+5&3%&30!51!3$%!,)),-8!P%%!D*4')%!E8"=8
=
#$%!reset()!1'&+3*5&!B'30!3$%!*&3%)&,.!,)),-!B5*&3%)!;,+J!,3!3$%!;%4*&&*&4!51!3$%!,)),-8!
#$%!?@?!(,&',.!0'44%030!'0*&4!3$*0!1'&+3*5&!35!)%0%3!3$%!,)),-!B5*&3%)7!;'3!*&!?@?!R!*3!*0!
&53!&%+%00,)-8
!
Figure 8.13. Saving the output from print_r() in a variable. Output from Example 8.9.
The var_dump() Function
The var_dump() function displays the array (or object), the number of elements, and the lengths of each of the string
values. It also provides output with indentation to show the structure of the array or object. (See Chapter 17, “Objects,”
for more on objects.)
Example 8.10 demonstrates the var_dump() function. The output is shown in Figure 8.14.
Format
void var_dump ( mixed expression [, mixed expression [, ]] )
!
Example:
$states=array('CA' => 'California','MT'=>'Montana','NY'=>'New York');
var_dump($states); // Dumps output in a structured format
Example 8.10.
<html>
<head><title>The var_dump() Function</title></head>
<body bgcolor="CCFF99">
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
<pre>
<b>
<?php
1 $colors=array('red','green', 'blue','yellow');
2 $book=array('Title' => 'War and Peace',
'Author' => 'Tolstoy',
'Publisher' => "Oxford University Press",
);
3 var_dump($colors);
var_dump($book);
?>
</b>
</pre>
</body>
</html>
Explanation
"
#$%!&'(%)*+!,)),-7!$colors7!*0!,00*4&%/!,!.*03!51!+5.5)08
9
#$%!,005+*,3*6%!,)),-7!$book7!*0!,00*4&%/!J%-Q6,.'%!B,*)08
=
#$%!var_dump()!1'&+3*5&!3,J%0!3$%!&,(%!51!3$%!,)),-!,0!*30!,)4'(%&37!,&/!
/*0B.,-0!3$%!5'3B'3!;-!1*)03!B)*&3*&4!3$%!&'(;%)!51!%.%(%&30!*&!3$%!,)),-7!*&/%:!
6,.'%7!,&/!3$%&!3$%!&'(;%)!51!+$,),+3%)0!*&!%,+$!51!3$%!,00*4&%/!03)*&4!6,.'%0!
,&/!*30!6,.'%8!P%%!D*4')%!E8"A8
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 8.14. Printing an array with the var_dump() function. Output Example 8.10.
!
8.1.4. Using Loops to Access Array Elements
Loops make it easy to step through an array. The for and while loops are useful for numeric arrays where you can
determine the size, and starting point of the array is usually 0, incremented by 1 for each element. The best way to loop
through an associative array is with the foreach loop, although you can use this loop for numerically indexed arrays
as well.
The for Loop
The for loop can be used to iterate through a numeric array. The initial value of the for loop will be the first index
value of the array, which will be incremented each time through the loop until the end of the array is reached.
Example 8.11 demonstrates how the for loop is used to view each element of an array. The output is displayed in
Figure 8.15.
Example 8.11.
<html>
<head><title>The for Loop</title></head>
<body bgcolor="lightgreen">
<table border="1" bordercolor="black" bgcolor="yellow">
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
<caption>Elements</caption>
<?php
1 $colors=array('red','green', 'blue','yellow');
2 for($i = 0; $i < count($colors); $i++){
3 echo "<tr><td><b>$colors[$i]</b></td></tr>";
}
?>
</table>
</body>
</html>
Explanation
"
#$%!&'(%)*+!,)),-7!$colors7!*0!,00*4&%/!,!.*03!51!+5.5)08
9
#$%!1*)03!,)4'(%&3!35!3$%!for!.55B7!$i = 07!0%30!3$%!*&*3*,.!6,.'%!51!$i!35!07!
2$*+$!)%B)%0%&30!3$%!1*)03!*&/%:!*&!3$%!,)),-8!K1!3$%!*&/%:!6,.'%7!$i7!*0!.%00!3$,&!
3$%!0*`%!51!3$%!,)),-!C)%3')&%/!1)5(!3$%!count()!1'&+3*5&F7!3$%!.55B!;5/-!*0!
%&3%)%/7!,13%)!/*0B.,-*&4!3$%!+5.5)7!3$%!3$*)/!,)4'(%&3!51!3$%!for!.55B!*0!
%:%+'3%/U!3$,3!*07!*&+)%(%&3!3$%!6,.'%!51!$i!;-!"8
=
O,+$!3*(%!3$)5'4$!3$%!.55B7!3$%!&%:3!%.%(%&3!*&!3$%!$colors!,)),-!*0!/*0B.,-%/!
*&!,!3,;.%!+% 8
Figure 8.15. Using the for loop to loop through an array. Output from Example 8.11.
!
!
The while Loop
The while loop can be used to iterate through a numeric array as shown in Examples 8.12 and 8.13.
By setting the initial value to 0, the loop will iterate from the first element of the array (assuming that the array starts at
element zero) until it reaches the end of the array. The count() or sizeof() functions can be used to find the
length of the array.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Example 8.12.
<html>
<head><title>The while Loop</title></head>
<body bgcolor="lightgreen">
<table border='1' bordercolor='black' bgcolor='yellow'>
<caption>Elements</caption>
<?php
1 $colors=array('red','white', 'aqua','yellow');
2 $i = 0;
3 while( $i < count($colors)){
4 echo "<tr bgcolor=$colors[$i]><td><b>$colors[$i]
</b></td></tr>";
5 $i++;
}
?>
</table>
</body>
</html>
Explanation
"
>!&'(%)*+!,)),-!+, %/!$colors!*0!+)%,3%/!,&/!,00*4&%/!03)*&4!6,.'%08
9
H,)*,;.%7!$i7!0%3!35!<7!2* !;%!3$%!*&*3*,.!6,.'%!*&!3$%!.55B7!,&/!2* !'0%/!,0!3$%!,)),-a0!
*&/%:8
=
#$%!count()!1'&+3*5&!)%3')&0!3$%!&'(;%)!51!%.%(%&30!*&!3$%!,)),-8!#$%!while!
%:B)%00*5&!3%030!3$,3!3$%!6,.'%!51!$i!*0!.%00!3$,&!3$%!0*`%!51!3$%!,)),-8
A
#$%!6,.'%!51!$i!2* !;%!'0%/!,0!3$%!*&/%:!6,.'%!51!3$%!,)),-7!$colors8!O,+$!3*(%!3$)5'4$!
3$%!.55B7!3$%!6,.'%!2* !;%!*&+)%(%&3%/!;-!"8!K&!3$*0!%:,(B.%7!3$%!6,.'%!51!3$%!%.%(%&37!
,!+5.5)7!2* !;%!'0%/!,0!3$%!;,+J4)5'&/!+5.5)!15)!3$%!+'))%&3!)527!,&/!,0!3$%!3%:3!
B)*&3%/!2*3$*&!3$%!3,;.%a0!/,3,!+% 8!P%%!D*4')%!E8"V8
R
#$%!6,.'%!51!$i!*0!*&+)%(%&3%/!;-!"8!K1!-5'!15)4%3!35!*&+)%(%&3!$i7!3$%!.55B!2* !45!
15)%6%)!;%+,'0%!$i!2* !,.2,-0!;%!.%00!3$,&!3$%!0*`%!51!3$%!,)),-8
!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 8.16. Using the while loop to iterate through an array. Output from Example 8.12.
Example 8.13.
G5/%!H*%2I!
<html><head><title>Table Colors</table></head>
<body bgcolor="blue">
<table border=1 bordercolor="white" align="center"
cellpadding="2">
<caption><b><font size="+2" color="yellow">Colored
Rows</font></b>
</caption>
<?php
1 $colors=array("orange","lightgreen", "lightblue","yellow");
2 $i=0;
3 while ( $i< 8 ){
// Each time through the loop the index value in the
array
// will be changed, with values 0, 1, 2, 3, 0, 1, 2, 3,
etc.
4 $color=$colors[$i%4];
?>
<tr bgcolor="<?=$color?>">
5 <td><?=$color?></td>
<td><?=$color?></td>
<td><?=$color?></td>
<td><?=$color?></td>
<td><?=$color?></td>
</tr>
<?php
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
[...]... Strings from Arrays and Arrays from Strings In Chapter 6, “Strings,” we discussed strings and their many functions Now that we have learned about arrays, PHP provides functions that serve the dual purpose of creating arrays from strings and strings from arrays (see Table 8.3) Table 8.3 Arrays and Strings Function What It Does explode() Splits up a string by a specified delimiter and creates an... by the as keyword and a variable to represent the key, called $key, followed by the => operator, and a variable to represent the value, called $value Figure 8.18 Looping through an array with the foreach loop Output from Example 8.14 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Modifying a Value by Reference with a foreach Loop As of PHP. .. is divided by 4 and the remainder (modulus) is replaced as the new index value 5 The first time in the loop the index is 0 The value $color[0] is "orange" and will be filled in the table for a row of 5 table cells See Figure 8.17 6 The value of $i is incremented by 1 Next time through the loop, $color[1] is "lightgreen" and that color... array of strings implode() Creates a string by gluing together array elements by a specific separator join() Alias for implode() split() Splits up a string into an array by a regular expression (see Chapter 12, “Regular Expressions and Pattern Matching”) The implode() Function The implode() function creates a string by joining together the elements of an array with... the associated array created by array_count_values() See Figure 8.23 Figure 8.23 The array_count_values() function Outpuf from Example 8.19 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 8.1.8 Extracting Keys and Values from Arrays PHP provides functions that allow you to extract elements from an array and assign the keys and values to variables The array_keys()... each() function returns the current key–value pair in an array, and moves to the next element, making that the current element The returned value is an array of two alternating values to represent each key and its corresponding value To access the array elements, you can use either 0 and 1 to represent each key and value, or the keywords key and value to do the same thing Used with a looping construct,... The each and list Functions < ?php echo "Using the list() and each() Functions"; echo ""; 1 $colors=array("red", "green", "blue"); 2 list($a,$b)=$colors;// Create two variables, $a and $b echo "The list() function assigns array elements to variables: "; echo 'list($a,$b)=$colors' "."; 3 echo "\$a == '$a' and \$b == '$b'.";... original array and the value of the number of times that element occurs in the array In the example, the color "red" appears three times, the color "blue" twice, and the colors "green" and "yellow" once The associative array, $unique_count, will contain keys representing the color element ("red", "blue", "green", "yellow") and the value associated... 7 This is the closing brace for the while loop Figure 8.17 The while loop and arrays Output from Example 8.13 The foreach Loop The foreach statement is designed to work with both numeric and associative arrays (works only on arrays) The loop expression consists of the array name, followed by the as keyword, and a user-defined variable that will hold each successive value of the array as the... array’s elements by preceding the value after the as keyword with & This will assign by reference instead of copying the value; that is, whatever you do to the array while in the loop will change the original array, not a copy of it Example 8.15 Code View: The foreach Loop Changing Values by Reference Original array < ?php 1 $val="hello"; . number or a string, and the value associated with it can be
a string, a number, another array, and so on. If an index value is not given, PHP provides a numeric. The for and while loops are useful for numeric arrays where you can
determine the size, and starting point of the array is usually 0, incremented by 1 for