Association array php

obatambeienwasirherbal.com: Arrays - Manual

Bạn đang xem: Association array php

obatambeienwasirherbal.com: Arrays - Manual
obatambeienwasirherbal.com: Arrays - Manual
Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Errors Exceptions Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apabịt module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding obatambeienwasirherbal.com Keeping Current Features HTTP authentication with obatambeienwasirherbal.com Cookies Sessions Dealing with XForms Handling tệp tin uploads Using remote files Connection handling Persistent Database Connections Commvà line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting obatambeienwasirherbal.com"s Behaviour Audio Formats Manipulation Authentication Services Comm& Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date và Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable và Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts? This help j Next thực đơn sản phẩm k Previous menu thành phầm g p Previous man page g n Next man page G Scroll lớn bottom g g Scroll khổng lồ top g h Goto homepage g s Goto lớn search(current page) / Focus tìm kiếm box
Iterables »
« Numeric strings
obatambeienwasirherbal.com Manual Language Reference Types
Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Romanian Russian Spanish Turkish Other
Submit a Pull Request Report a Bug

Arrays

An array in obatambeienwasirherbal.com is actually an ordered maps. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, các mục (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, và probably more. As array values can be other array s, trees and multidimensional array s are also possible.

Explanation of those data structures is beyond the scope of this manual, but at least one example is provided for each of them. For more information, look towards the considerable literature that exists about this broad topic.


Syntax


Specifying with array()

An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments.

array( key => value , key2 => value2 , key3 => value3 , ... )

The comma after the last array element is optional & can be omitted. This is usually done for single-line arrays, i.e. array(1, 2) is preferred over array(1, 2, ). For multi-line arrays on the other hand the trailing comma is commonly used, as it allows easier addition of new elements at the over.

Note:

A short array syntax exists which replaces array() with <>.


Example #1 A simple array


$array = array(
    
"foo" => "bar",
    
"bar" => "foo",
);

// Using the short array syntax
$array = <
    
"foo" => "bar",
    
"bar" => "foo",
>;
?>

Xem thêm: Dbrr Là Gì - Rẻ Rách Hay Giẻ Rách

The key can either be an int or a string . The value can be of any type.

Additionally the following key casts will occur: String s containing valid decimal int s, unless the number is preceded by a + sign, will be cast to the int type. E.g. the key "8" will actually be stored under 8. On the other h& "08" will not be cast, as it isn't a valid decimal integer. Float s are also cast khổng lồ int s, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8. Bool s are cast to int s, too, i.e. the key true will actually be stored under 1 & the key false under 0. Null will be cast lớn the empty string, i.e. the key null will actually be stored under "". Array s và object s can not be used as keys. Doing so will result in a warning: Illegal offphối type.

If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.


Example #2 Type Casting và Overwriting example


$array = array(
    
1    => "a",
    
"1"  => "b",
    
1.5  => "c",
    
true => "d",
);
var_dump($array);
?>

The above example will output:


array(1)

As all the keys in the above sầu example are cast khổng lồ 1, the value will be overwritten on every new element và the last assigned value "d" is the only one left over.


obatambeienwasirherbal.com.. arrays can contain int & string keys at the same time as obatambeienwasirherbal.comhường does not distinguish between indexed và associative arrays.


Example #3 Mixed int & string keys


$array = array(
    
"foo" => "bar",
    
"bar" => "foo",
    
100   => -100,
    -
100  => 100,
);
var_dump($array);
?>

The above example will output:


array(4)

The key is optional. If it is not specified, obatambeienwasirherbal.com will use the increment of the largest previously used int key.


Example #4 Indexed arrays without key


$array = array("foo""bar""hello""world");
var_dump($array);
?>