<?php
$scores = [70, 90, 80];
$updatedScorfes = [];
foreach ($scores as $score){
$updatedScores[] = $score + 5;
}
print_r($updatedScores);
カテゴリー: PHP
PHP 配列とforeach
<?php
$scores = [70, 90, 80];
foreach($scores as $key => $value){
echo “Score{{$key}}: {$value}” . PHP_EOL;
}
PHP 配列の要素を変数に代入
<?php
// $scores = [70, 90, 80];
// $firstScore = $scores[0];
// $secondScore = $scores[1];
// $thirdScore = $scores[2];
// list($firstScore, $secondScore, $thirdScore) = $scores;
// [$firstScore, $secondScore, $thirdScore] = $scores;
// echo $firstScore . PHP_EOL;
// echo $secondScore . PHP_EOL;
// echo $thirdScore . PHP_EOL;
$x = 10;
$y = 20;
[$y, $x] = [$x, $y];
echo $x . PHP_EOL;
echo $y . PHP_EOL;
PHP 配列の要素を入れ替える
<?php
$scores = [70, 90, 80];
// sort($scores);
// print_r($scores);
// rsort($scores);
// print_r($scores);
// asort($scores);
// print_r($scores);
// arsort($scores);
// print_r($scores);
// shuffle($scores);
// print_r($scores);
$reversed = array_reverse($scores);
print_r($reversed);
PHP array_splice
<?php
$scores = [70, 90, 80];
array_splice($scores, 1, 0, [30, 20]);
array_splice($scores, 2, 1);
$removedItems = array_splice($scores, 1, 1, [10, 15]);
print_r($scores);
print_r($removedItems);
PHP 配列の要素
<?php
$scores = [70, 90, 80];
$scores[] = 60;
$moreScores = [10, 20, 30, ...$scores];
print_r($moreScores);
PHP in_array、array_search
<?php
$names = ["Taro", "Jiro", "Saburo"];
echo array_search("Jiro", $names) .PHP_EOL;
PHP 配列
<?php
$scores = [70, 90, 80];
// echo $scores[0] . PHP_EOL;
// $scores[1] = 95;
// echo $scores[1] . PHP_EOL;
var_dump($scores);
print_r($scores);
PHP 無名関数
<?php
// function triple($num)
// {
// return $num * 3;
// }
// $triple = function($num)
// {
// return $num * 3;
// };
$triple = fn($num) => $num * 3;
echo $triple(10) . PHP_EOL;
PHP 関数
<?php
function showAd()
{
echo "---------" . PHP_EOL;
echo "SALE! 50% OFF!" . PHP_EOL;
echo "---------" . PHP_EOL;
}
function showContent()
{
echo "BREAKING NEWS!" . PHP_EOL;
echo "Two baby pandas born at our Zoo!" . PHP_EOL;
}
showAd();
showContent();
showAd();