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 array
Array
Specify the value directly without specifying the key.The key element starts with “0” from the beginning.
Specify the key to store the value.
Stores multiple key-specified values. Use “=>”.
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.
The foreach when there is a subscript is as follows.
list
Use list if you want to receive an array from the function as a return value.
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>