PHP의 배열은 실제로 순서가 지정된 맵입니다. 맵은 값을 키에 연결하는 유형입니다. 이 유형은 여러 가지 방법으로 최적화되어 있으므로 실제 배열 또는 목록(벡터), 해시 테이블(맵 구현), 사전, 세트, 스택, 큐 등으로 처리할 수 있습니다. 더 많은 가능성. 배열 요소의 값은 다른 배열일 수도 있으므로 트리 구조와 다차원 배열도 허용됩니다.
이러한 구조를 설명하는 것은 이 매뉴얼의 범위를 벗어나지만 각 구조에 대해 최소한 하나의 예가 제공됩니다. 이러한 구조에 대한 자세한 내용은 이 광범위한 주제에 대한 다른 연구를 참조하는 것이 좋습니다.
구문
배열 정의 array()
array() 언어 구조를 사용하여 새 배열을 만들 수 있습니다. 쉼표로 구분된 키 => 값 쌍을 원하는 만큼 허용합니다.
array( key => value
, ...
)
// key는 정수 또는 문자열일 수 있습니다. string
// value )은 임의의 값일 수 있습니다. type
마지막 배열 요소 뒤의 쉼표는 생략 가능합니다. 일반적으로 array(1, 2, ) 대신 array(1, 2)와 같은 단일 행 배열 정의에 사용됩니다. 새 셀을 더 쉽게 추가할 수 있도록 여러 줄 배열 정의에서 마지막 쉼표를 남겨 두는 것이 일반적입니다.
5.4부터는 array() 대신 []를 사용하여 짧은 배열 정의 구문을 사용할 수 있습니다.
예제 #1 간단한 배열
<?php $array = array( "foo" => "bar", "bar" => "foo", ); // 自 PHP 5.4 起 $array = [ "foo" => "bar", "bar" => "foo", ]; ?>
키는 정수 또는 문자열일 수 있습니다. 값은 모든 유형이 될 수 있습니다.
또한 key에는 다음과 같은 강제가 적용됩니다.
적법한 정수 값이 포함된 문자열은 정수로 변환됩니다. 예를 들어 키 이름 "8"은 실제로 8로 저장됩니다. 그러나 "08"은 합법적인 십진수 값이 아니기 때문에 캐스팅되지 않습니다.
부동 소수점 숫자도 정수로 변환됩니다. 즉, 소수 부분은 반올림됩니다. 예를 들어 키 이름 8.7은 실제로 8로 저장됩니다.
부울 값도 정수로 변환됩니다. 즉, 키 이름 true는 실제로 1로 저장되고 키 이름 false는 0으로 저장됩니다.
Null은 빈 문자열로 변환됩니다. 즉, 키 이름 null은 실제로 ""로 저장됩니다.
배열과 객체는 키로 사용할 수 없습니다. 이렇게 하면 다음과 같은 경고가 발생합니다: 잘못된 오프셋 유형.
여러 장치가 배열 정의에서 동일한 키 이름을 사용하는 경우 마지막 하나만 사용되며 이전 항목을 덮어씁니다.
예제 #2 유형 강제 변환 및 덮어쓰기의 예
<?php $array = array( 1 => "a", "1" => "b", 1.5 => "c", true => "d", ); var_dump($array); ?>
위 루틴은 다음을 출력합니다.
array(1) { [1]=> string(1) "d" }
위 예의 모든 키 이름은 1로 강제됩니다. , 그러면 각각의 새 단위가 이전 값을 덮어쓰고 하나의 "d"만 남게 됩니다.
PHP는 실제로 인덱스 배열과 연관 배열을 구별하지 않기 때문에 PHP 배열에는 정수와 문자열 키 이름이 모두 포함될 수 있습니다.
주어진 값에 키가 지정되지 않은 경우 현재 가장 큰 정수 인덱스 값이 사용되며 새 키 이름은 해당 값에 1을 더한 값이 됩니다. 지정된 키 이름에 이미 값이 있으면 해당 값을 덮어씁니다.
예제 #3 정수 및 문자열 키 이름 혼합
<?php $array = array( "foo" => "bar", "bar" => "foo", 100 => -100, -100 => 100, ); var_dump($array); ?>
위 루틴은 다음을 출력합니다.
array(4) { ["foo"]=> string(3) "bar" ["bar"]=> string(3) "foo" [100]=> int(-100) [-100]=> int(100) }
key는 선택 사항입니다. 지정하지 않으면 PHP는 자동으로 이전에 사용된 가장 큰 정수 키에 1을 더한 값을 새 키로 사용합니다.
예제 #4 키 이름이 없는 인덱스 배열
<?php $array = array("foo", "bar", "hallo", "world"); var_dump($array); ?>
위 루틴은 다음을 출력합니다.
array(4) { [0]=> string(3) "foo" [1]=> string(3) "bar" [2]=> string(5) "hallo" [3]=> string(5) "world" }
특정 단위에 대해서만 키 이름을 지정할 수도 있습니다. 다른 빈 공간:
예 #5 일부 장치에 대한 키 이름만 지정
<?php $array = array( "a", "b", 6 => "c", "d", ); var_dump($array); ?>
위 루틴은 다음을 출력합니다.
array(4) { [0]=> string(1) "a" [1]=> string(1) "b" [6]=> string(1) "c" [7]=> string(1) "d" }
마지막 항목을 볼 수 있습니다. 값 "d"에는 키 이름 7이 자동으로 할당됩니다. 이전에 가장 큰 정수 키가 6이었기 때문입니다.
대괄호 구문을 사용하여 배열 셀에 액세스
배열 셀은 array[key] 구문을 통해 액세스할 수 있습니다.
예제 #6 배열 셀 액세스
<?php $array = array( "foo" => "bar", 42 => 24, "multi" => array( "dimensional" => array( "array" => "foo" ) ) ); var_dump($array["foo"]); var_dump($array[42]); var_dump($array["multi"]["dimensional"]["array"]); ?>
위 루틴은 다음을 출력합니다.
string(3) "bar" int(24) string(3) "foo"
참고:
대괄호와 중괄호는 다음과 같습니다. 확인 배열 요소에 액세스하려면 교대로 사용합니다(예: $array[42] 및 $array{42}는 위 예에서 동일한 효과를 가집니다).
PHP 5.4부터 배열을 사용하여 함수 또는 메소드 호출 결과를 간접적으로 참조할 수 있습니다. 이전에는 임시 변수만 전달할 수 있었습니다.
PHP 5.5부터 배열 간접 참조를 사용하여 배열 프로토타입을 참조하는 것이 가능합니다.
예 #7 배열 간접 참조
<?php function getArray() { return array(1, 2, 3); } // on PHP 5.4 $secondElement = getArray()[1]; // previously $tmp = getArray(); $secondElement = $tmp[1]; // or list(, $secondElement) = getArray(); ?>
참고:
정의되지 않은 배열 키에 액세스하려는 시도는 정의되지 않은 변수에 액세스하는 것과 동일합니다. E_NOTICE 수준이 발생합니다. 오류 메시지, 결과는 NULL입니다.
대괄호 구문을 사용하여 생성/수정
해당 값을 명시적으로 설정하여 기존 배열을 수정할 수 있습니다.
대괄호 안에 키 이름을 지정하여 배열에 값을 할당하면 됩니다. 키 이름을 생략할 수도 있습니다. 이 경우 변수 이름에 빈 대괄호([]) 쌍을 추가합니다.
$arr[key] = value;
$arr[] = value;
// key 可以是 integer 或 string
// value 可以是任意类型的值
如果 $arr 还不存在,将会新建一个,这也是另一种新建数组的方法。不过并不鼓励这样做,因为如果 $arr 已经包含有值(例如来自请求变量的 string)则此值会保留而 [] 实际上代表着字符串访问运算符。初始化变量的最好方式是直接给其赋值。。
要修改某个值,通过其键名给该单元赋一个新值。要删除某键值对,对其调用 unset() 函数。
<?php $arr = array(5 => 1, 12 => 2); $arr[] = 56; // This is the same as $arr[13] = 56; // at this point of the script $arr["x"] = 42; // This adds a new element to // the array with key "x" unset($arr[5]); // This removes the element from the array unset($arr); // This deletes the whole array ?>
Note:
如上所述,如果给出方括号但没有指定键名,则取当前最大整数索引值,新的键名将是该值加上 1(但是最小为 0)。如果当前还没有整数索引,则键名将为 0。
注意这里所使用的最大整数键名不一定当前就在数组中。它只要在上次数组重新生成索引后曾经存在过就行了。以下面的例子来说明:
<?php // 创建一个简单的数组 $array = array(1, 2, 3, 4, 5); print_r($array); // 现在删除其中的所有元素,但保持数组本身不变: foreach ($array as $i => $value) { unset($array[$i]); } print_r($array); // 添加一个单元(注意新的键名是 5,而不是你可能以为的 0) $array[] = 6; print_r($array); // 重新索引: $array = array_values($array); $array[] = 7; print_r($array); ?>
以上例程会输出:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Array ( ) Array ( [5] => 6 ) Array ( [0] => 6 [1] => 7 )
实用函数
有很多操作数组的函数,参见数组函数一节。
Note:
unset() 函数允许删除数组中的某个键。但要注意数组将不会重建索引。如果需要删除后重建索引,可以用 array_values() 函数。
?php $a = array(1 => 'one', 2 => 'two', 3 => 'three'); unset($a[2]); /* will produce an array that would have been defined as $a = array(1 => 'one', 3 => 'three'); and NOT $a = array(1 => 'one', 2 =>'three'); */ $b = array_values($a); // Now $b is array(0 => 'one', 1 =>'three') ?>
foreach 控制结构是专门用于数组的。它提供了一个简单的方法来遍历数组。
数组做什么和不做什么
为什么 $foo[bar] 错了?
应该始终在用字符串表示的数组索引上加上引号。例如用 $foo['bar'] 而不是 $foo[bar]。但是为什么呢?可能在老的脚本中见过如下语法:
<?php $foo[bar] = 'enemy'; echo $foo[bar]; // etc ?>
这样是错的,但可以正常运行。那么为什么错了呢?原因是此代码中有一个未定义的常量(bar)而不是字符串('bar'-注意引号),而 PHP 可能会在以后定义此常量,不幸的是你的代码中有同样的名字。它能运行,是因为 PHP 自动将裸字符串(没有引号的字符串且不对应于任何已知符号)转换成一个其值为该裸字符串的正常字符串。例如,如果没有常量定义为 bar,PHP 将把它替代为 'bar' 并使用之。
Note: 这并不意味着总是给键名加上引号。用不着给键名为常量或变量的加上引号,否则会使 PHP 不能解析它们。
<?php error_reporting(E_ALL); ini_set('display_errors', true); ini_set('html_errors', false); // Simple array: $array = array(1, 2); $count = count($array); for ($i = 0; $i < $count; $i++) { echo "\nChecking $i: \n"; echo "Bad: " . $array['$i'] . "\n"; echo "Good: " . $array[$i] . "\n"; echo "Bad: {$array['$i']}\n"; echo "Good: {$array[$i]}\n"; } ?>
以上例程会输出:
Checking 0:
Notice: Undefined index: $i in /path/to/script.html on line 9
Bad:
Good: 1
Notice: Undefined index: $i in /path/to/script.html on line 11
Bad:
Good: 1
Checking 1:
Notice: Undefined index: $i in /path/to/script.html on line 9
Bad:
Good: 2
Notice: Undefined index: $i in /path/to/script.html on line 11
Bad:
Good: 2
演示此行为的更多例子:
<?php // Show all errors error_reporting(E_ALL); $arr = array('fruit' => 'apple', 'veggie' => 'carrot'); // Correct print $arr['fruit']; // apple print $arr['veggie']; // carrot // Incorrect. This works but also throws a PHP error of level E_NOTICE because // of an undefined constant named fruit // // Notice: Use of undefined constant fruit - assumed 'fruit' in... print $arr[fruit]; // apple // This defines a constant to demonstrate what's going on. The value 'veggie' // is assigned to a constant named fruit. define('fruit', 'veggie'); // Notice the difference now print $arr['fruit']; // apple print $arr[fruit]; // carrot // The following is okay, as it's inside a string. Constants are not looked for // within strings, so no E_NOTICE occurs here print "Hello $arr[fruit]"; // Hello apple // With one exception: braces surrounding arrays within strings allows constants // to be interpreted print "Hello {$arr[fruit]}"; // Hello carrot print "Hello {$arr['fruit']}"; // Hello apple // This will not work, and will result in a parse error, such as: // Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING' // This of course applies to using superglobals in strings as well print "Hello $arr['fruit']"; print "Hello $_GET['foo']"; // Concatenation is another option print "Hello " . $arr['fruit']; // Hello apple ?>
当打开 error_reporting 来显示 E_NOTICE 级别的错误(将其设为 E_ALL)时将看到这些错误。默认情况下 error_reporting 被关闭不显示这些。
和在语法一节中规定的一样,在方括号(“[”和“]”)之间必须有一个表达式。这意味着可以这样写:
<?php echo $arr[somefunc($bar)]; ?>
这是一个用函数返回值作为数组索引的例子。PHP 也可以用已知常量,可能之前已经见过:
<?php $error_descriptions[E_ERROR] = "A fatal error has occured"; $error_descriptions[E_WARNING] = "PHP issued a warning"; $error_descriptions[E_NOTICE] = "This is just an informal notice"; ?>
注意 E_ERROR 也是个合法的标识符,就和第一个例子中的 bar 一样。但是上一个例子实际上和如下写法是一样的:
<?php $error_descriptions[1] = "A fatal error has occured"; $error_descriptions[2] = "PHP issued a warning"; $error_descriptions[8] = "This is just an informal notice"; ?>
因为 E_ERROR 等于 1,等等。
那么为什么这样做不好?
也许有一天,PHP 开发小组可能会想新增一个常量或者关键字,或者用户可能希望以后在自己的程序中引入新的常量,那就有麻烦了。例如已经不能这样用 empty 和 default 这两个词了,因为他们是保留字。
Note: 重申一次,在双引号字符串中,不给索引加上引号是合法的因此 "$foo[bar]" 是合法的(“合法”的原文为 valid。在实际测试中,这么做确实可以访问数组的该元素,但是会报一个常量未定义的 notice。无论如何,强烈建议不要使用 $foo[bar]这样的写法,而要使用 $foo['bar'] 来访问数组中元素。--haohappy 注)。至于为什么参见以上的例子和字符串中的变量解析中的解释。
转换为数组
对于任意 integer,float,string,boolean 和 resource 类型,如果将一个值转换为数组,将得到一个仅有一个元素的数组,其下标为 0,该元素即为此标量的值。换句话说,(array)$scalarValue 与 array($scalarValue) 完全一样。
如果一个 object 类型转换为 array,则结果为一个数组,其单元为该对象的属性。键名将为成员变量名,不过有几点例外:整数属性不可访问;私有变量前会加上类名作前缀;保护变量前会加上一个 '*' 做前缀。这些前缀的前后都各有一个 NULL 字符。这会导致一些不可预知的行为:
<?php class A { private $A; // This will become '\0A\0A' } class B extends A { private $A; // This will become '\0B\0A' public $AA; // This will become 'AA' } var_dump((array) new B()); ?>
上例会有两个键名为 'AA',不过其中一个实际上是 '\0A\0A'。
将 NULL 转换为 array 会得到一个空的数组。
比较
可以用 array_diff() 和数组运算符来比较数组。
示例
PHP 中的数组类型有非常多的用途。以下是一些示例:
<?php // This: $a = array( 'color' => 'red', 'taste' => 'sweet', 'shape' => 'round', 'name' => 'apple', 4 // key will be 0 ); $b = array('a', 'b', 'c'); // . . .is completely equivalent with this: $a = array(); $a['color'] = 'red'; $a['taste'] = 'sweet'; $a['shape'] = 'round'; $a['name'] = 'apple'; $a[] = 4; // key will be 0 $b = array(); $b[] = 'a'; $b[] = 'b'; $b[] = 'c'; // After the above code is executed, $a will be the array // array('color' => 'red', 'taste' => 'sweet', 'shape' => 'round', // 'name' => 'apple', 0 => 4), and $b will be the array // array(0 => 'a', 1 => 'b', 2 => 'c'), or simply array('a', 'b', 'c'). ?>
Example #8 使用 array()
<?php // Array as (property-)map $map = array( 'version' => 4, 'OS' => 'Linux', 'lang' => 'english', 'short_tags' => true ); // strictly numerical keys $array = array( 7, 8, 0, 156, -10 ); // this is the same as array(0 => 7, 1 => 8, ...) $switching = array( 10, // key = 0 5 => 6, 3 => 7, 'a' => 4, 11, // key = 6 (maximum of integer-indices was 5) '8' => 2, // key = 8 (integer!) '02' => 77, // key = '02' 0 => 12 // the value 10 will be overwritten by 12 ); // empty array $empty = array(); ?>
Example #9 集合
<?php $colors = array('red', 'blue', 'green', 'yellow'); foreach ($colors as $color) { echo "Do you like $color?\n"; } ?>
以上例程会输出:
Do you like red?
Do you like blue?
Do you like green?
Do you like yellow?
直接改变数组的值自 PHP 5 起可以通过引用传递来做到。之前的版本需要需要采取变通的方法:
Example #10 在循环中改变单元
<?php // PHP 5 foreach ($colors as &$color) { $color = strtoupper($color); } unset($color); /* ensure that following writes to $color will not modify the last array element */ // Workaround for older versions foreach ($colors as $key => $color) { $colors[$key] = strtoupper($color); } print_r($colors); ?>
以上例程会输出:
Array ( [0] => RED [1] => BLUE [2] => GREEN [3] => YELLOW )
本例生成一个下标从 1 开始的数组。
Example #11 下标从 1 开始的数组
<?php $firstquarter = array(1 => 'January', 'February', 'March'); print_r($firstquarter); ?>
以上例程会输出:
Array
(
[1] => 'January'
[2] => 'February'
[3] => 'March'
)
Example #12 填充数组
<?php // fill an array with all items from a directory $handle = opendir('.'); while (false !== ($file = readdir($handle))) { $files[] = $file; } closedir($handle); ?>
数组是有序的。也可以使用不同的排序函数来改变顺序。更多信息参见数组函数。可以用 count() 函数来数出数组中元素的个数。
Example #13 数组排序
<?php sort($files); print_r($files); ?>
因为数组中的值可以为任意值,也可是另一个数组。这样可以产生递归或多维数组。
Example #14 递归和多维数组
<?php $fruits = array ( "fruits" => array ( "a" => "orange", "b" => "banana", "c" => "apple" ), "numbers" => array ( 1, 2, 3, 4, 5, 6 ), "holes" => array ( "first", 5 => "second", "third" ) ); // Some examples to address values in the array above echo $fruits["holes"][5]; // prints "second" echo $fruits["fruits"]["a"]; // prints "orange" unset($fruits["holes"][0]); // remove "first" // Create a new multi-dimensional array $juices["apple"]["green"] = "good"; ?>
数组(Array) 的赋值总是会涉及到值的拷贝。使用引用运算符通过引用来拷贝数组。
<?php $arr1 = array(2, 3); $arr2 = $arr1; $arr2[] = 4; // $arr2 is changed, // $arr1 is still array(2, 3) $arr3 = &$arr1; $arr3[] = 4; // now $arr1 and $arr3 are the same ?>

의존성 (di) inphpenhancescodeflexibility 및 testability는 decouplingdependencycreation fromusage.toimplementDieffectically : 1) addicontainersjudicuelyToavoidover-Engineering.2) indhe. 3) adhe

toimproveyourphpwebsite의 성능, UsetheseStrospations : 1) ubstractOpCodeCachingWithOpCaceToSpeedUpscriptScriptIngretation.2) 최적화 된 AabaseQueriesBysElectingOnlynecessaryFields.3) UsecachingsystemsLikeredSormcedUcedUcedUcedALOW

예, itispossibletosendmassemailswithphp.1) uselibraries -lifephpmailerorswiftmailerforfficialemailsending.2) emubledelaysbetemailstoavoidspamflags.3) personalizeemailsingdynamiccontenttoimproveengement.4) usequeuesystemslikerbitmbitmquredisb

의존성 (di) inphpisadesignpatternthatachievesinversionofcontrol (ioc) by ancelociestobeinjectedintoclasses, 향상 모듈 성, 테스트 가능성 및 flexibility.didecouplesssclassessfromspecificimplementations, codemoremanageableandadapt

PHP를 사용하여 이메일을 보내는 가장 좋은 방법은 다음과 같습니다. 1. 기본 전송에 Php 's Mail () 함수를 사용합니다. 2. phpmailer 라이브러리를 사용하여 더 복잡한 HTML 메일을 보내십시오. 3. Sendgrid와 같은 트랜잭션 메일 서비스를 사용하여 신뢰성 및 분석 기능을 향상시킵니다. 이러한 방법을 사용하면 이메일이받은 편지함에 도달 할뿐만 아니라 수신자를 유치 할 수 있습니다.

PHP 다차원 어레이에서 총 요소 수를 계산하는 것은 재귀 적 또는 반복적 인 방법을 사용하여 수행 할 수 있습니다. 1. 재귀 방법은 배열을 가로 지르고 중첩 배열을 재귀 적으로 처리함으로써 계산됩니다. 2. 반복 방법은 스택을 사용하여 깊이 문제를 피하기 위해 재귀를 시뮬레이션합니다. 3. Array_Walk_Recursive 함수도 구현할 수 있지만 수동 계산이 필요합니다.

PHP에서, do-while 루프의 특성은 루프 본체가 적어도 한 번 실행되도록하고 조건에 따라 루프를 계속할지 여부를 결정하는 것입니다. 1) 조건부 점검 전에 루프 본체를 실행하며, 사용자 입력 확인 및 메뉴 시스템과 같이 작업을 적어도 한 번 수행 해야하는 시나리오에 적합합니다. 2) 그러나, do-while 루프의 구문은 초보자들 사이에서 혼란을 야기 할 수 있으며 불필요한 성능 오버 헤드를 추가 할 수 있습니다.

PHP의 효율적인 해싱 스트링은 다음 방법을 사용할 수 있습니다. 1. 빠른 해싱에 MD5 기능을 사용하지만 비밀번호 저장에는 적합하지 않습니다. 2. SHA256 기능을 사용하여 보안을 향상시킵니다. 3. Password_hash 함수를 사용하여 비밀번호를 처리하여 최고 보안과 편의성을 제공하십시오.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

드림위버 CS6
시각적 웹 개발 도구

VSCode Windows 64비트 다운로드
Microsoft에서 출시한 강력한 무료 IDE 편집기

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경