Summary
Let’s arrange the array in PHP.
In addition, we will also describe the sequence processing.
If you haven’t done so last time, please refer to the following.
PHP Part01 Variables and Operations
PHP array
Array
Specify the value directly without specifying the key.The key element starts with “0” from the beginning.
Directly
$temp_array = array('A', 'B', 'C');
print ($temp_array[0]);
print ($temp_array[1]);
print ($temp_array[2]);
結果
ABC
Specify the key to store the value.
Key specification
$temp_array['A'] = 'Apple';
$temp_array['B'] = 'Strawberry';
$temp_array['C'] = 'Banana';
print ($temp_array['A']);
print ($temp_array['B']);
print ($temp_array['C']);
結果
AppleStrawberryBanana
Stores multiple key-specified values. Use “=>”.
Multiple implementations
$temp_array = [
'aaa' => 'Apple',
'bbb' => 'Strawberry',
'ccc' => 'Banana'
];
print ($temp_array['aaa']);
print ($temp_array['bbb']);
print ($temp_array['ccc']);
結果
AppleStrawberryBanana
Array processing
This is a convenient control syntax that processes the data stored in the array in order.
foreach
Loops for the number of data stored in the array.
foreach
$temp_array = array('A', 'B', 'C');
foreach($temp_array as $temp){ // 配列の中身をtempへ渡す
print($temp); // tempの中身が表示されます
print("<br/>");
}
結果
A
B
C
The foreach when there is a subscript is as follows.
Subscripted array foreach
$temp_array = [
'aaa' => 'Apple',
'bbb' => 'Strawberry',
'ccc' => 'Banana'
];
//Pass the key to tempKey and the value to tempValue
foreach($temp_array as $tempkey => $tempValue){
// The contents of temp will be displayed
print($tempkey.'='.$tempValue);
print("<br/>");
}
結果
aaa=Apple
bbb=Strawberry
ccc=Banana
list
Use list if you want to receive an array from the function as a return value.
list
function array_func(){
return array('A', 'B', 'C');
}
// Store return value array in list variable
list($a, $b, $c) = array_func();
print ($a);
print ($b);
print ($c);
print("<br/>");
結果
ABC
This code
<html>
<head>
<title>PHP</title>
</head>
<body>
<?php
/* 配列 */
$temp_array = array('A', 'B', 'C');
print ($temp_array[0]); // The contents of the array are displayed
print ($temp_array[1]); // The contents of the array are displayed
print ($temp_array[2]); // The contents of the array are displayed
print("<br/>");
/* key */
$temp_array['A'] = 'Apple';
$temp_array['B'] = 'Strawberry';
$temp_array['C'] = 'Banana';
print ($temp_array['A']); // The contents of the array are displayed
print ($temp_array['B']); // The contents of the array are displayed
print ($temp_array['C']); // The contents of the array are displayed
print("<br/>");
/* => */
$temp_array = [
'aaa' => 'Apple',
'bbb' => 'Strawberry',
'ccc' => 'Banana'
];
print ($temp_array['aaa']); // The contents of the array are displayed
print ($temp_array['bbb']); // The contents of the array are displayed
print ($temp_array['ccc']); // The contents of the array are displayed
print("<br/>");
$temp_array = array('A', 'B', 'C');
foreach($temp_array as $temp){ // Pass the contents of the array to temp
print($temp); // The contents of temp are displayed
print("<br/>");
}
/* foreach */
$temp_array = [
'aaa' => 'Apple',
'bbb' => 'Strawberry',
'ccc' => 'Banana'
];
// Pass the key to tempKey and the value to tempValue
foreach($temp_array as $tempkey => $tempValue){
// tempの中身が表示されます
print($tempkey.'='.$tempValue);
print("<br/>");
}
/* list */
function array_func(){
return array('A', 'B', 'C');
}
list($a, $b, $c) = array_func();
print ($a);
print ($b);
print ($c);
print("<br/>");
?>
</body>
</html>