WONKWANG UNIVERSITY
Web Programming
ARRAY
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 2
CONTENTS
ARRAY
One dimensional array (numeric arrays)
Associative arrays
Multi-dimensional array
Array functions
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 3
One dimensional array
Array : An array stores multiple values in one single variable.
An array has same data type of the value and same name for the variable.
A variable is a storage area holding a number or text.
Component of array: key (or index) + value
Example
<?php
$score[0] = 85;
$score[1] = 75;
$score[2] = 80;
$score[3] = 90;
$score[4] = 85;
for ( $i=0; $i<5; $i++ ) {
echo "\$score[$i] = $score[$i] <BR>";
}
?>
$score[0] $score[1] $score[2] $score[3]
85 75 80 90 85 $score[4]
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 4
One dimensional array(2)
Case of omitting key value
example
<?php
$score[ ] = 85;
$score[ ] = 75;
$score[3] = 80;
$score[5] = 90;
$score[ ] = 65;
$score[2] = 68;
$score[1] = 88;
$score[ ] = 95;
foreach ( $score as $key => $val ) {
echo "\$score[$key] = $val <BR>";
}
?>
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 5
Associative arrays
Array with string type key value
each ID key is associated with a value.
Ex
<?php
$player['pitcher'] = "박찬호";
$player['catcher'] = "이만수";
$player['first'] = "최희섭";
$player['second'] = "안경현";
$player['third'] = "김동주";
echo "{$player['pitcher']} <BR>";
echo "{$player['catcher']} <BR>";
echo "{$player['first']} <BR>";
echo "{$player['second']} <BR>";
echo "{$player['third']} <BR>";
?>
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 6
Multidimensional arrays
each element in the main array can also be an array. And each
element in the sub-array can be an array, and so on
$score[1][0] $score[1][1] $score[1][2] $score[1][3]
85 75 80 90
$score[0][0] $score[0][1] $score[0][2] $score[0][3]
75 65 70 80
<?php
$score[0][0] = 85; $score[0][1] = 75; $score[0][2] = 80; $score[0][3] = 90;
$score[1][0] = 75; $score[1][1] = 65; $score[1][2] = 70; $score[1][3] = 80;
printf("A 선수의 총점 = %d <BR>",$score[0][0]+$score[1][0]);
printf("B 선수의 총점 = %d <BR>",$score[0][1]+$score[1][1]);
printf("C 선수의 총점 = %d <BR>",$score[0][2]+$score[1][2]);
printf("C 선수의 총점 = %d <BR>",$score[0][3]+$score[1][3]);
?>
[0][ ]
[1][ ]
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 7
Multidimensional arrays
$score[1][0] $score[1][1] $score[1][2] $score[1][3]
85 75 80 90
$score[0][0] $score[0][1] $score[0][2] $score[0][3]
75 65 70 80
<?php
$score[0][0] = 85; $score[0][1] = 75; $score[0][2] = 80; $score[0][3] = 90;
$score[1][0] = 75; $score[1][1] = 65; $score[1][2] = 70; $score[1][3] = 80;
printf("A 선수의 총점 = %d <BR>",$score[0][0]+$score[1][0]);
printf("B 선수의 총점 = %d <BR>",$score[0][1]+$score[1][1]);
printf("C 선수의 총점 = %d <BR>",$score[0][2]+$score[1][2]);
printf("C 선수의 총점 = %d <BR>",$score[0][3]+$score[1][3]);
?>
[0][ ]
[1][ ]
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 8
Array functions
Array function used
example
<?php
$score[0] = 85;
$score[1] = 75;
$score[2] = 80;
$score[3] = 90;
$score[4] = 85;
for ( $i=0; $i<5; $i++ ) {
echo "\$score[$i] = $score[$i] <BR>"; }
foreach ( $score as $key => $value ) {
echo “\$score[ $key ] = $value <BR> “; }
?>
result ?
<?php
$score = array(85, 75, 80, 90, 85);
$score = array(0=>85, 1=>75, 2=>80, 3=>90, 4=>85);
for ( $i=0; $i<5; $i++ ) {
echo "\$score[$i] = $score[$i] <BR>";
}
?>
result ?
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 9
Array functions
Example using string
<?php
$player = array(„pitcher‟=>”박찬호”, „catcher‟=>”이만수”);
echo “{\$player[„pitcher‟] = $player[„pitcher‟] }<BR>”;
echo “{\$player[„pitcher‟] = $player[„catcher‟] }<BR>”;
//키가 문자열이면 출력시에도 문자열로
?>
result ?
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 10
Array functions(2)
Two dimensional array
$score[1][0] $score[1][1] $score[1][2] $score[1][3]
85 75 80 90
$score[0][0] $score[0][1] $score[0][2] $score[0][3]
75 65 70 80
<?php
$score[0][0] = 85; $score[0][1] = 75; $score[0][2] = 80; $score[0][3] = 90;
$score[1][0] = 75; $score[1][1] = 65; $score[1][2] = 70; $score[1][3] = 80;
$scoreNew = array( 0=>array(85, 75, 80, 90), 1=>array(75, 65, 70, 80) );
printf("A의 점수 = %d <BR>",$score[0][0]+$score[1][0]);
printf("B의 점수 = %d <BR>",$score[0][1]+$score[1][1]);
printf("C의 점수 = %d <BR>",$score[0][2]+$score[1][2]);
printf(“D의 점수 = %d <BR>",$score[0][3]+$score[1][3]);
for ( $i=0; $i<2; $i++ ) {
for ( $j=0; $j<4; $j++ ) {
printf("A 선수의 총점 = %d <BR>", $scoreNew[$i][$j]); } }
?>
[0][ ]
[1][ ]
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 11
Array functions(3)
List : function to replace values returned from array with general
variables at a time
ex
<?php
$info = array( "Database", "Query", "DBMS" );
list( $data, $lang, $soft ) = $info;
echo "\$data=$data, \$lang=$lang, \$soft=$soft <BR>";
list( $data, , $soft ) = $info;
echo "\$data=$data, \$soft=$soft <BR>";
list( , , $soft ) = $info;
echo "\$soft=$soft <BR>";
?>
result?
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 12
Array functions(4)
Each : facilitates to refer the values in an array through cursor
function
return array with 4 elements format ( 0, 1, “key”, “value” )
0, “key” : key value of array element
1, “value” : values stored in array
Return false when the data getting by each is last
format :
returned-type each ( array-type-returend array_name) : array each ( array
array )
Ex
(each function)
<?php
$arr = array ( "수학", "국어", "영어", "체육", "생물", “물리” );
$element = each ( $arr );
echo "$element[0] {$element[ 'key„ ] } <BR>";
echo "$element[1] {$element[ 'value„ ] } <BR>";
$element = each ( $arr );
echo "$element[0] {$element[ 'key‟ ] } <BR>";
echo "$element[1] {$element[ 'value‟ ] } <BR>";
?>
result ?
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 13
Array functions(5)
Each :
ex (with string key value)
<?php
$arr = array ( "cf"=>"안정환", "mf"=>"박지성", "df"=>"이을용”);
$element = each ( $arr );
echo "$element[0] {$element['key']} <BR>";
echo "$element[1] {$element['value']} <BR>";
$element = each ($arr);
echo "$element[0] {$element['key']} <BR>";
echo "$element[1] {$element['value']} <BR>";
?>
result ?
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 14
Array functions(6)
Print the values of an array using loop statements
<?php
$arr = array ("cf"=>"안정환", "mf"=>"박지성", "df"=>"이을용");
while ( list( $key, $value ) = each ( $arr ) ) {
echo "$key : $value <BR>";
}
// OR
$arr = array ("cf"=>"안정환", "mf"=>"박지성", "df"=>"이을용");
foreach ( $arr as $key => $value ) {
echo "$key : $value <BR>";
}
?>
result ?
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 15
Array functions(7)
Count : return number of count of array elements
format :
returned-type count ( mixed var [, int mode ]) :
ex) int count (mixed var [, int mode])
<?php
$arr = array( "이현창", "이윤하", "이하윤“ );
$num = count( $arr ); // sizeof( $arr);
echo "원소의 개수 : $num <BR>";
for ( $i=0; $i<$num; $i++ ) {
list( $key,$value ) = each( $arr );
echo "$key : $value <BR>";
}
?>
result?
<?php
// 이차원 배열
$food = array( '한식' => array('곰탕', '불고기', '해장국', '냉면'),
'양식' => array('햄버거', '피자','커틀렛','스테이크'),
'중식' => array('짜장면', '짬뽕', '탕수육','양장피'));
echo count( $food )."<BR>";
echo count( $food,COUNT_RECURSIVE )."<BR>";
var_dump( $food );
?>
result ?
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 16
Array functions(8)
Sort : sort array elements
format :
void sort (array array [, int sort_flags] )
SORT_REGULAR : comparison of general elements
SORT_NUMERIC : comparison with numeric elements
SORT_STRING : comparison with string elements
ex
<?php
$product = array
("MP3", "PDA", "DICA", "NOTEBOOK");
foreach ( $product as $key => $value ) {
echo "$key : $value <BR>";
}
sort ( $product );
echo "<HR>";
foreach ( $product as $key => $value ) {
echo "$key : $value <BR>";
}
?>
result ?
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 17
Array functions(9)
Methods preserving key order with string type key
<?php
$score = array
("lee" => 90, "kim"=>40, "choi"=>80, "park
"=>85);
sort ($score);
foreach ( $score as $key => $value ) {
echo "$key : $value <BR>";
}
?>
result ?
<?php
$score = array
("lee" => 90, "kim"=>40, "choi"=>80, "park"
=>85);
asort ($score);
foreach ( $score as $key => $value ) {
echo "$key : $value <BR>";
}?>
result?
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 18
Array functions(10)
in_array : distinguish whether there is a specific value or not.
format : bool in_array ( mixed needle, array haystack [, bool strict ] )
If there is needle when searching the word needle in array haystack, return true
otherwise false.
If strict is set true, then it is case-sensitive comparison processed.
<?php
$arr = array ("cass", "lager", "corona", "adams", "hite", "miller");
if ( in_array ("cass", $arr) ) echo "cass was found <BR>";
if ( in_array ("Miller", $arr) ) echo "Miller was found <BR>";
echo "<HR>";
$arr = array ( "3.14", 256, 512, 1048, 2048);
if ( in_array ( 3.14, $arr) )
echo "3.14 was found <BR>";
if ( in_array ( 3.14, $arr, TRUE) )
echo "3.14 was found strictly<BR>";
if ( in_array ( "3.14", $arr, TRUE) )
echo "\"3.14\" was found strictly<BR>";
?>
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 19
Array functions(11)
Cursor functions in array operation(internal pointer)
current : return a current element value of array
format : mixed current ( array array )
next :
format : mixed next ( array array )
prev :
format : mixed prev ( array array )
end :
format : mixed end ( array array )
reset
format : mixed reset ( array array )
key :
format : mixed key ( array array )
WONKWANG UNIVERSITY Division of Information and e-Commerce - HCL 20
The End
ARRAY
One dimensional array (numeric arrays)
Associative arrays
Multi-dimensional array
Array functions