Array Php 7

obatambeienwasirherbal.com: Arrays - Manual

Bạn đang xem: Array php 7

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.comhường Keeping Current Features HTTP authentication with obatambeienwasirherbal.comhường Cookies Sessions Dealing with XForms Handling tệp tin uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting obatambeienwasirherbal.com"s Behaviour Audio Formats Manipulation Authentication Services Comm& Line Specific Extensions Compression & Archive sầu Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language & Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output đầu ra Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts? This help j Next menu cửa nhà k Previous thực đơn thành quả g p Previous man page g n Next man page G Scroll khổng lồ bottom g g Scroll to lớn top g h Gokhổng lồ homepage g s Golớn search(current page) / Focus search 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 map. A bản đồ is a type that associates values khổng lồ keys. This type is optimized for several different uses; it can be treated as an array, danh sách (vector), hash table (an implementation of a map), dictionary, collection, staông chồng, queue, và probably more. As array values can be other array s, trees và 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 and 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: Kiến Thức Cơ Bản Và Cách Tạo Wbs Là Gì ? Những Ý Nghĩa Của Wbs

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 khổng lồ the int type. E.g. the key "8" will actually be stored under 8. On the other hand "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 lớn int s, too, i.e. the key true will actually be stored under 1 & the key false under 0. Null will be cast to 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 offset 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 & 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 to lớn 1, the value will be overwritten on every new element & the last assigned value "d" is the only one left over.


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


Example #3 Mixed int và 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);
?>