What Are PHP Arrays?

Author Topic: What Are PHP Arrays?  (Read 511 times)

Offline chinmay.sahooTopic starter

  • Trade Count: (0)
  • Full Member
  • ***
  • Thank You 2
  • Posts: 139
  • Karma: 0
    • No Addiction
What Are PHP Arrays?
« on: 12-21-2015, 02:25:13 »
PHP arrays are associative arrays with a little extra machinery thrown in. The associative part means that arrays store element values in association with key values rather than in a strict linear index order. (If you have seen arrays in other programming languages, they are likely to have been vector arrays rather than associative arrays—see the related sidebar for an explanation of the difference.) If you store an element in an array, in association with a key, all you need to retrieve it later from that array is the key value. For example, storage is as
simple as this:

$state_location[‘San Mateo’] = ‘California’;

which stores the element ‘California’ in the array variable $state_location, in association with the lookup key ‘San Mateo’. After this has been stored, you can look up the stored value by using the key, like so:

$state = $state_location[‘San Mateo’]; // equals ‘California’

Simple, no?

If all you want arrays for is to store key/value pairs, the preceding information is all you need to know. Similarly, if you want to associate a numerical ordering with a bunch of values, all you have to do is use integers as your key values, as in:

$my_array[1] = “The first thing”;
$my_array[2] = “The second thing”; // and so on ...

For Perl programmers: Arrays in PHP are much like hashes in Perl, with some syntactic differences. For one thing, all variables in PHP are denoted with a leading $, not just scalar variables. Second, even though the array is associative, the indices are grouped by square brackets ([]) rather than curly braces ({}). Finally, there is no array or list type indexed only by integers. The convention is to use integers as associative indices, and the array itself maintains an internal ordering for iteration purposes.

Offline RH-Calvin

  • Trade Count: (0)
  • Hero Member
  • *****
  • Thank You 49
  • Posts: 1010
  • Karma: -1
  • Gender: Male
    • Cheap VPS Hosting
Re: What Are PHP Arrays?
« Reply #1 on: 12-21-2015, 23:51:03 »
An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.


Related Topics

  Subject / Started by Replies Last post
0 Replies
Last post 09-27-2016, 06:20:03
by beingchinmay