본문 바로가기

Webstoryboy

Category

Explanation

REFERENCE/PHP

[PHP] PHP 배열

PHP 배열

배열은 여러개의 값을 변수에 저장 할 수 있습니다.


PHP 배열

배열은 여러개의 값을 변수에 저장 할 수 있습니다.

$변수명 = array();

PHP 배열 사용방법

PHP 배열 사용방법 예제입니다.

Script
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>php</title>
    <script>
        const arr1 = new Array();
        arr1[0] = 100;
        arr1[1] = 200;

        document.write(arr1[0], "<br>")
        document.write(arr1[1], "<br><br>")

        const arr2 = [];
        arr2[0] = 100;
        arr2[1] = 200;

        document.write(arr2[0], "<br>")
        document.write(arr2[1], "<br><br>")

        const arr3 = new Array(100, 200);

        document.write(arr3[0], "<br>")
        document.write(arr3[1], "<br><br>")

        const arr4 = [100, 200];

        document.write(arr4[0], "<br>")
        document.write(arr4[1], "<br><br>")

        const arr5 = [100, 200];
        document.write(arr5.length, "<br><br>")

        const arr6 = [100, 200, 300];
        for( let i=0; i<arr6.length; i++){
            document.write(arr6[i], "<br>");
        }
        document.write("<br><br>")

        const arr7 = [100, 200, 300, 400];
        const map7 = arr7.map(x => x);
        document.write(map7,"<br><br>")

        const arr8 = [100, 200, 300, 400];
        const map8 = arr8.map(x => x * 2);
        document.write(map8,"<br><br>")
    </script>
</head>
<body>
    <?php
        $array1 = array();

        $array1[0] = 1000;
        $array1[1] = 2000;

        echo $array1[0];
        echo "<br>";
        echo $array1[1];
        echo "<br><br>";

        $array2 = array();

        $array2["숫자1"] = 1000;
        $array2["숫자2"] = 2000;

        echo $array2["숫자1"];
        echo "<br>";
        echo $array2["숫자2"];
        echo "<br><br>";

        $array3 = array();

        array_push($array3, 1000, 2000, 3000, 4000);

        echo $array3[0]. "<br>";
        echo $array3[1]. "<br>";
        echo $array3[2]. "<br>";
        echo $array3[3]. "<br><br>";

        $array4 = array();

        $array4["숫자"][0] = 1000;
        $array4["숫자"][1] = 2000;
        $array4["숫자"][2] = 3000;
        $array4["숫자"][3] = 4000;
        $array4["문자"][1] = "천";
        $array4["문자"][2] = "이천";
        $array4["문자"][3] = "삼천";
        $array4["문자"][4] = "사천";

        var_dump($array4);
        echo "<br><br>";

        $array5 = array();
        $array5 = [1000,2000,3000,4000,5000];

        echo $array5[0];
        echo $array5[1];
        echo $array5[2];
        echo $array5[3];
        echo $array5[4];
    ?>
</body>
</html>

AD

comments