PHP
[PHP] 배열
헤이봄봄
2022. 6. 14. 18:42
배열의 크기
<html>
<body>
<?php
$l = [1, 2, 3, 4, 5];
echo count($l);
?>
</body>
</html>
// 5
배열 조작
push, pop, shift, unshift
<html>
<body>
<?php
$arr = ['a', 'b', 'c', 'd', 'e'];
array_push($arr, 'f');
var_dump($arr);
?>
</body>
</html>
// array(6) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" [4]=> string(1) "e" [5]=> string(1) "f" }
배열 정렬
sort, rsort(반대 정렬)
<html>
<body>
<?php
$li = ['c', 'a', 'b'];
sort($li);
var_dump($li);
?>
</body>
</html>
// array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" }
기타 공식문서
https://www.php.net/manual/en/ref.array.php
PHP: Array Functions - Manual
While PHP has well over three-score array functions, array_rotate is strangely missing as of PHP 5.3. Searching online offered several solutions, but the ones I found have defects such as inefficiently looping through the array or ignoring keys. The follow
www.php.net
연관배열의 생성
// (1)
<html>
<body>
<?php
$grades = array('egoing'=>10, 'k8805'=>6, 'sorialgi'=>80);
var_dump($grades)
?>
</body>
</html>
// array(3) { ["egoing"]=> int(10) ["k8805"]=> int(6) ["sorialgi"]=> int(80) }
// (2)
<html>
<body>
<?php
$grades = [];
$grades['egoing'] = 10;
$grades['k8805'] = 6;
$grades['sorialgi'] = 80;
var_dump($grades);
?>
</body>
</html>
배열의 키로 값 가져오기
<html>
<body>
<?php
$grades = array('egoing'=>10, 'k8805'=>6, 'sorialgi'=>80);
echo $grades['sorialgi'];
?>
</body>
</html>