From 6ce8429d4567c046fd96aa18d8edc2cda9def4dc Mon Sep 17 00:00:00 2001 From: NirvashPrime <37520562+NirvashPrime@users.noreply.github.com> Date: Tue, 16 Oct 2018 00:55:52 -0400 Subject: [PATCH] Revised document and expanded scope of information (#19430) Removed text copied from php.net, revised grammar and formatting, and expanded scope of information to include multiple array styles and how to access elements in these styles. --- .../pages/guide/english/php/array/index.md | 96 ++++++++++++++----- 1 file changed, 71 insertions(+), 25 deletions(-) diff --git a/client/src/pages/guide/english/php/array/index.md b/client/src/pages/guide/english/php/array/index.md index e23aaf7f11..84a6290b41 100644 --- a/client/src/pages/guide/english/php/array/index.md +++ b/client/src/pages/guide/english/php/array/index.md @@ -2,39 +2,85 @@ title: array --- -## Introduction of PHP array +## Introduction to PHP Array -An array in PHP is actually an ordered map. 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, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. -As array values can be other arrays, trees and multidimensional arrays are also possible. +An array can be thought of as a collection of items. +## Syntax + +An array is defined by array(), or []. + +An example of an array in each style can be seen below: -Here is an example: ``` + +$bikes = array('Suzuki','BMW','Yamaha'); ``` - -PHP array has so many functions to work with. Here is all list sorted: Functions - -## Associative arrays - -PHP arrays can be used as key and value like map. It can be accessed by key too. - -Here is an simple example: ``` "bar", - "bar" => "foo", -); - -echo $array['bar']; +$bikes = ['Suzuki', 'BMW', 'Yamaha']; ``` -Have a good day, happy coding !!! +## Key => Value + +Arrays can also be defined with named keys, as shown below: + +``` + 'Suzuki', + 'second favorite' => 'BMW', + 'not my favorite' => 'Yamaha' +]; +``` + +## Accessing Items + +Items within an array can be accessed by their corresponding key, or location within the array. + +For instance: + +``` + 'Suzuki', + 'second favorite' => 'BMW', + 'not my favorite' => 'Yamaha' +]; + +echo 'I like '. $bikes['not my favorite'] +``` + +Would produce the following output: + +``` +I like BWM +``` + +## Pitfalls + +When working with arrays, there are a few important things to keep in mind: + +1) A comma after the last element is optional. +2) Named keys must be escaped to be accessed (i.e. $bikes[not my favorite] would not work). + +For more information, please see [PHP: Arrays](http://php.net/manual/en/language.types.array.php)