Cooly Wizardy
Cooly Wizardy

Reputation: 191

What are Arrays in PHP

As per standard Array definition: An array is a variable which allows you to store multiple value of same Data Type.

In PHP, say $a=array("abc",123,"4");

What will be $a[0] abc , $a[1] 123 and $a[2] 4 treated as String, Numeric value or character value and Why?

Upvotes: 0

Views: 105

Answers (4)

Arslan Zulfiqar
Arslan Zulfiqar

Reputation: 1

Arrays

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.

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 end.

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
?>

Upvotes: 0

hjpotter92
hjpotter92

Reputation: 80639

PHP treats array differently from general definition of them.

I suggest you read more about them on the official docs.

Upvotes: 1

tereško
tereško

Reputation: 58444

In php arrays are not arrays.

If you really want to know how this whole thing works, i would recommend for you to watch this presentations: PHP UK Conference 2012 - PHP under the hood, by Johannes Schlüter.

Also, as @RepWhoringPeeHaa mentioned : use var_dump().

Upvotes: 2

flo
flo

Reputation: 2018

Arrays in PHP are different, there's only one type of array which an store different types. in Your example the items keep their original type. abc is a String, 123 is a number and 4 is a string.

You can even have nonnumeric keys. For Example $a["a"] = "test".

Upvotes: 3

Related Questions