简体   繁体   中英

What kind of array does PHP use?

I can define an array in PHP like this:

$array = array();

In C++, we have two kinds of array.

  1. The first kind is a fixed size array, for example:

     int arr[4]; // 4 ints, hardcoded size
  2. The second kind is a dynamic sized array

    std::vector<int> v; // can grow and shrink at runtime

What kind of array does PHP use? Are both kinds of arrays in PHP? If so, can you give me examples?

PHP is not as strict as C or C++. In PHP you don't need to specify the type of data to be placed in an array, you don't need to specify the array size either.

If you need to declare an array of integers in C++ you can do it like this:

int array[6];

This array is now bound to only contain integers. In PHP an array can contain just about everything:

$arr = array();
$arr[] = 1;
$arr[] = 2;
$arr[] = 3;
$arr[] = 4;
var_dump($arr);   //Prints [1,2,3,4]
$arr[] = 'hello world';   //Adding a string. Completely valid code
$arr[] = 3.14;   //Adding a float. This one is valid too
$arr[] = array(
           'id' => 128,
           'firstName' => 'John'
           'lastName' => 'Doe'
);  //Adding an associative array, also valid code
var_dump($arr);  //prints [1,2,3,4,'hello world',3.14, [ id => 128, firstName => 'John', lastName => 'Doe']]

If you're coming from a C++ background it's best to view the PHP array as a generic vector that can store everything.

From php.net

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.

Basically there are three Usage patterns of array in PHP.

Indexed array: Arrays with sequential numeric index, such as 0, 1, 2, etc. Example:

$myarray = array();
$myarray[0] = "test data 1";
$myarray[1] = "test data 2";
$myarray[3] = "test data 3";

Associative array: This is the most frequently used type of PHP arrays whose elements are defined in key/value pair. Example:

$myarray = array();
$myarray["key1"] = "value 1";
$myarray["key2"] = "value 2";
$myarray["key3"] = "value 3";

Multidimensional array: Arrays whose elements may contains one or more arrays. There is no limit in the level of dimensions. Example:

$myarray = array();
$myarray[0] = array("data01","data02","data03");
$myarray[1] = array("data11","data12","data13");

For more details - Refer to PHP 5 Arrays .

PHP uses three kinds of array:

Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion.

Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.

Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices.

Numeric Array Ex:

 $numbers = array( 1, 2, 3, 4, 5);

Associative Array Ex:

$salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);

Multidimensional Array Ex:

$marks = array( 
            "mohammad" => array (
               "physics" => 35,
               "maths" => 30,   
               "chemistry" => 39
            ),

            "qadir" => array (
               "physics" => 30,
               "maths" => 32,
               "chemistry" => 29
            ),

            "zara" => array (
               "physics" => 31,
               "maths" => 22,
               "chemistry" => 39
            )
         );

A php array, in C++ terms, is roughly:

std::map< std::experimental::any, std::experimental::any >

where std::experimental::any is a type that can hold basically anything. The php equivalent also can be sorted with the equivalent of < .

Well not quite -- closer to the truth is that a php array is an abstract interface that exposes much of the operations that the above map would provide in C++ (the C++ map is a concrete implementation).

Arrays with contiguous numeric keys stored in the Variant are treated much like a std::vector<Variant> , and under the interface the php system might even use vector<Variant> or something similar to store it, or even have two different internal details, one of which is for contiguous blocks of integer indexed data, and the other for sparse entries. (I don't know how php is implemented, but that is how I would do it)

PHP uses numeric, associative arrays, and multidimensional arrays. Arrays are dynamic in nature, and no size should be mentioned. Go through php.net/manual/en/language.types.array.php to find details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM