This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2108-Php.txt
181 lines (116 loc) · 4.28 KB
/
2108-Php.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
Array in PHP
-------------------------------------------------------------------
There are two "types" (kind) of "PHP" arrays:
1. Indexed array : Elements are accessed via zero based integer index.
2. Associative or Map or Dictionary array : Unique string "key" will be used to set/get an element into/from arrays.
To create an array
-------------------
We've have "array()" construct and subscript operator [] to create arrays.
$varName = []; or $varName = array();
will creates an empty array.
You can use same constructs to initialize an array -- Array initializer syntax.
$varName = [value1,value2,...];
or $varName = array(value1,value2,value3,....);
To dump array content for debug purpose --- print_r construct will be used.
---------------------------------------------------------------------
<?php
$ar = [10,20,40];
$ar1 = array("Meh",10,"Pat",20.30);
$ar2 = array(10,array("A","B",array("P","Q","R)));
print_r($ar);
print_r($ar1);
print_r($ar2);
?>
NOTE: To understand the structure of array or examine the arrays we've to use print_r keyword.
Open "Source View" from web-browser to see the debug data produced by print_r.
Creating Map or Assoc (Associative) array
------------------------------------------
Syntax:
$varName = ["key1"=>value1, "key2"=>value2,...];
or $varName = array("key1"=>value1, "key2"=>value2,...);
or $varName = array();
$varName["key1"] = value1;
...
Example:
<?php
$emp = array("id"=> 10, "name"=> "Rajesh", "salary"=>4000.34);
$std = array();
$std["roll"] = 30;
$std["name"] = "Mr.A";
print_r($emp);
print_r($std);
echo "<br/>", $std["roll"], $std["name"];
?>
Append an element as "indexed" elements
---------------------------------------------------------
Syntax:
$varName[]=value;
^
|--- subscript must be empty
"value" will be added at 0 index if an array is empty. It will insert an element at last_index + 1 otherwise.
<?php
$cities = array();
$cities[] = "Meh"; //0 index
$cities[] = "Pat"; //1 index
$cities[10] = "Raj"; //10 index;
$cities[] = "Jam"; //11 index
print_r($cities);
echo "<br/>", count($cities); // returns size of array
?>
int count(array()) returns size of array (or count of elements). To traverse or iterate array we cannot use simple "for" counter loop due to inconsistency in array index. So do not use while, for and do while loop to traverse arrays.
The foreach --- array iterator loop
-----------------------------------------------------------------
Syntax - 1
foreach($arrayVarName as $key_or_index=>$value)
{
//statements
}
Syntax - 2
foreach($arrayVarName as $value)
{
//
}
The "foreach" scans array element one after one, store index/key and value to $key=>value auto variables and inside the loop read values from $key and $value variables.
<?php
$nos = [10,20,30];
foreach($nos as $index=>$value)
{
echo "<br/>", $index, $value);
}
echo "<br/> Without key or index";
foreach($nos as $no){
echo "<br/>", $no;
}
?>
[NOTE: You can use any variablename to address the $key=>$value pair]
Example: Store roll,name and marks of five subject of three students.
<?php
$ar = array();
$ar[] = array(10,"Raj", array(33,44,55,66,77));
$ar[] = array(11,"Man", array(83,44,77,66,77));
$ar[] = array(12,"Tan", array(93,64,55,66,77));
print_r($ar);
foreach($ar as $std)
{
echo "<br/>Roll : ", $std[0], "Name : ", $std[1];
echo " Marks : " , $std[2][0], $std[2][1], $std[2][2],$std[2][3],$std[2][4];
}
foreach($ar as $std)
{
echo "<br/>Roll : ", $std[0], "Name : ", $std[1];
foreach($std[2] as $mark){
echo "\n<br/>\t\t", $mark;
}
}
?>
Embed array expression in double quoted & HEREDOC string
----------------------------------------------------------------
Do not include double quotes while accessing "assoc" array elements in string.
<?php
$emp = array("no"=>10, "name"=>"Raj");
$str = "I'm $emp[name] and $emp[no] is my roll";
echo $str;
//or use string concat operator
$str = "I'm " . $emp["name"] . " and " . $emp["no"] . " is my roll";
echo $str;
?>