PHP 배열 함수

WBOY
풀어 주다: 2024-08-29 12:44:50
원래의
532명이 탐색했습니다.

PHP 배열 함수(Hypertext Pre-processor의 약어)는 널리 사용되는 범용 스크립트 언어입니다. HTML 및 웹 개발에 적합한 호환성은 이해해야 할 중요한 기술입니다. PHP에서 배열은 단일

에 여러 값을 보유하거나 저장할 수 있는 변수 유형을 의미합니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

HTML에 쉽게 삽입할 수 있습니다. 간략하게 살펴보겠습니다.

코드:

<!DOCTYPE html>
<html>
<head>
<title>……………</title>
</head>
<body>
<?php
echo  "Hello, I am a PHP !";
?>
</body>
</html>
로그인 후 복사

출력:

PHP 배열 함수

위 스크립팅 파일은 PHP 스크립트가 HTML과의 호환성을 얼마나 잘 유지하는지 보여주는 매우 명확한 증거입니다. PHP 코드에는 특별한 시작 및 끝 괄호가 포함되어 있습니다.

PHP에서 배열을 만드는 방법은 무엇입니까?

배열()

아래에는 어레이 작동 방식이 나열되어 있습니다.

$color = array("red", "green", "blue");
로그인 후 복사

출력

$color[0] = “빨간색”
$color[1] = “녹색”
$color[2] = “파란색”

여기서는 색상 이름을 하나의 단일 색상 변수에 저장하려는 의도입니다. 그래서 배열 함수에 색상 변수가 있는데 이 함수에서는 모든 색상을 문자열 형식으로 하나씩 이름을 지정했습니다.

PHP 배열 함수

배열에는 3가지 유형이 있습니다.

  • 숫자형 배열
  • 연관배열
  • 다차원 배열

이 세 가지에 대한 설명은 다음과 같습니다.

1. 숫자형 배열

숫자 배열은 숫자 인덱스가 있는 배열입니다. 숫자 배열의 구문을 살펴보겠습니다. 구문에는 두 가지 유형이 있습니다.

첫 번째 방법:

$array_name[0] = value;
로그인 후 복사

두 번째 방법:

$array_name[] = value;
로그인 후 복사
참고: 여기서 대괄호 안의 0 [0]은 인덱스 번호를 나타냅니다.

값은 사용자가 배열에 저장하고 싶은 것을 의미합니다.

첫 번째와 두 번째 구문에는 약간의 차이가 있습니다. 하나는 []에 0이 있고 다른 하나는 공백 []입니다.

기본적으로 모든 배열은 인덱스 0으로 시작합니다. 즉, 첫 번째 배열의 경우 []에 0을 입력하거나 공백으로 두는 경우([] 모두 동일함을 의미함) 차이점을 더 잘 이해하려면 예를 하나 더 살펴보세요

$array_name[] = value; {either you put 0 or leave it blank – both means same}
$array_name [1] = value;
로그인 후 복사

아래에는 값과 인덱스가 다른 배열이 나열되어 있습니다.

$name[0] = "Alex";
$name[1] = "Peter";
$name[2] = "Lucy"
로그인 후 복사

2. 연관배열

연관 배열은 문자열을 인덱스로 갖는 배열입니다. 저장된 값은 선형 인덱싱이 아닌 키 값과 연관되어 수행됩니다.

연관배열의 구문을 살펴보겠습니다.

$array_name["key"] = value;
로그인 후 복사
참고: 키 또는 인덱스라고 부릅니다(둘 다 동일한 의미를 가짐).

연관배열은 값과 키(또는 인덱스) 사이의 관계를 만들어야 할 때 사용됩니다.

3. 다차원 배열

다차원 배열은 하나 이상의 배열과 값을 포함하는 배열입니다. 이러한 배열은 다중 인덱스로 액세스됩니다.

단일 정의에서는 다차원을 배열의 배열이라고 부를 수 있습니다. 다차원 배열은 1D(1차원), 2D(2차원) ….n차원이 될 수 있습니다.

Alex England 23
Peter Germany 26
Lucy Holland 27

따라서 2D로 저장하면 할당은 아래와 같습니다.

Alex [0][0] England[0][1] 23[0][2]
Peter[1][0] Germany[1][1] 26[1][2]
Lucy[2][0] Holland[2][1] 27[2][2]

The same goes for ‘n’ number of dimensions and allocations.

Examples on Types of the Array Function

Let us see the types of the array with the help of an example:

1. Numeric Array

Code:

<html>
<body>
<?php
$numbers[] = "eleven";
$numbers[] = "twelve";
$numbers[] = "thirteen";
$numbers[] = "fourteen";
$numbers[] = "fifteen";
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
?>
</body>
</html>
로그인 후 복사

Output:

PHP 배열 함수

2. Associative Array

Code:

<html>
<body>
<?php
$salaries['Alex'] = "high";
$salaries['Peter'] = "medium";
$salaries['Lucy'] = "low";
echo "Salary of Alex is ". $salaries['Alex'] . "<br />";
echo "Salary of Peter is ". $salaries['Peter']. "<br />";
echo "Salary of Lucy is ". $salaries['Lucy']. "<br />";
?>
</body>
</html>
로그인 후 복사

Output:

PHP 배열 함수

3. Multidimensional array

Code:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Multidimensional Array</title>
</head>
<body>
<?php
// Define nested array
$contacts = array(
array(
"name" => "Petergomes",
"email" => "[email protected]",
),
array(
"name" => "Clark anthony",
"email" => "[email protected]",
),
array(
"name" => "lucy disilva",
"email" => "[email protected]",
)
);
// Access nested value
echo "Peter gomes's Email-id is: " . $contacts[0]["email"];
?>
</body>
</html>
로그인 후 복사

Output:

PHP 배열 함수

Advantages

Following are some of the advantages described.

  • When your intention is to represent multiple data that belong to the same type with using only single indexing naming.
  • It has wide applicability as it can be used to implement other data structures like stacks, trees, queues, graphs, and linked lists.
  • 2D/3D arrays are used to represent matrices effectively
  • It has less coding with the elimination of complexity
  • Sorting can be done easily

Conclusion

PHP arrays hold crucial importance in PHP programming, it acts as the ultimate variable of PHP. It behaves as a storage container for collecting elements. Arrays also can store other variables within like strings, integers, and even other arrays. If you have to deal with an unknown amount of variables you must prefer to work using arrays. Loops can be used to output values in arrays, also by simply calling specific elements with the index or key values.

위 내용은 PHP 배열 함수의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
php
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!