Friday, July 24, 2020

PHP: Arrays

An array is like a list of values. The simplest form of an array is indexed by integer, and ordered by the index, with the first element lying at index 0.

$foo = array(1, 2, 3); // An array of integers

$bar = ["A", true, 123 => 5]; // Short array syntax, PHP 5.4+

echo $bar[0]; // Returns "A"

echo $bar[1]; // Returns true

echo $bar[123]; // Returns 5

echo $bar[1234]; // Returns null

Arrays can also associate a key other than an integer index to a value. In PHP, all arrays are associative arrays
behind the scenes, but when we refer to an 'associative array' distinctly, we usually mean one that contains one or
more keys that aren't integers.

$array = array( );

$array[ "foo" ] = "bar";

$array[ "baz" ] = "quux";

$array[42] = "hello";
echo $array[ "foo" ]; // Outputs "bar"
echo $array [ "bar" ]; // Outputs "quux"
echo $array[42]; // Outputs "hello"


Source: PHP Notes for Professionals by Goal Kicker - Internet Archive Data
  • rss
  • Del.icio.us
  • Digg
  • Twitter
  • StumbleUpon
  • Reddit
  • Share this on Technorati
  • Post this to Myspace
  • Share this on Blinklist
  • Submit this to DesignFloat

0 comments:

Post a Comment