Can PHP arrays hold items of different type?

17,939

Solution 1

According to the PHP manual you can indeed store heterogeneous types inside a PHP "array" - scroll down to example 3.

Note that even though the example is about keys being ints or strings, the values assigned in the example are also both ints and strings, demonstrating that it is possible to store heterogeneous types.

Be aware that in the case of different-typed keys there is automatic casting involved so you may have surprising results in the case where e.g. a string contains a valid decimal representation.

Solution 2

In PHP arrays aren't even arrays, they're ordered hash-tables.

Solution 3

Yes. A PHP array can have multiple data types in it. Also, you should note that arrays in PHP actually are represented in the form of key-value pairs, where the elements you will input into the array are values. You can explicitly define keys too, when entering elements into the array, but when you don't, PHP will use indices starting from 0. Example:

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

PHP will interpret as

 array(4) {
  ["foo"]=>
  string(3) "bar"
  ["bar"]=>
  string(3) "foo"
  [100]=>
  int(-100)
  [-100]=>
  int(100)
}

Reference- http://php.net/manual/en/language.types.array.php

Solution 4

Not going to put oil on the fire of the PHP Arrays are no arrays here… But yes, you can put different variable types (string, int, …) together in a PHP thing called Array.

Share:
17,939
Rolen Koh
Author by

Rolen Koh

Nothing to see here

Updated on June 11, 2022

Comments

  • Rolen Koh
    Rolen Koh about 2 years

    In other programming languages the definition of arrays is something which can hold similar kind of elements. For example if I declare something like int i[] it will store integers, but in PHP a single array seems to be holding strings and numbers together.

    Will the number/integer be treated as string in such type of array in PHP?

  • yannis
    yannis almost 12 years
    Well ordered hash-tables are associative arrays, so PHP arrays are arrays. Don't know what definition of array you have in mind, but the limitation that arrays can only hold items of the same type is a language limitation, not a conceptual one.
  • Florian Margaine
    Florian Margaine almost 12 years
    @YannisRizos I suggest you read this post to see why people don't consider php arrays as real arrays technosophos.com/content/php-arrays-are-not-arrays It's not only about the type, it's also about the ordering (inexistent in PHP)
  • K..
    K.. almost 12 years
    Isn't an array a datastructure, where all the values are of the same size and ordered one after one in memory? What you're refering sounds like a dictionary/map to me. The only thing I know, that associative arrays have in common with real arrays is a part of the name.
  • yannis
    yannis almost 12 years
    @FlorianMargaine That article talks about "arrays in the traditional sense", yet the link under the sentence points to the Wikipedia article on the array data type, with a very simple definition: "In computer science, an array type is a data type that is meant to describe a collection of elements (values or variables), each selected by one or more indices (identifying keys) that can be computed at run time by the program". That's it, and that's all there is to it, ordering (that's accessible to the developer) is not part of the data structure's definition.
  • yannis
    yannis almost 12 years
    @FlorianMargaine Furthermore, ordering exists in PHP arrays. It might be inaccessible to the developer under certain circumstances, but inaccessible != inexistent. What the author of the article and perhaps you have in mind as "array" is just one of the many implementations of the concept. But let's not confuse concepts with implementations, and who cares about "real arrays" anyway?
  • Admin
    Admin almost 12 years
    @da_b0uncer: It means when somebody asks what are arrays in PHP I can answer they are ordered hash-tables and do not necessarily store same kind of values in them but can contain different types of values?
  • yannis
    yannis almost 12 years
    @RolenKoh Did you read the manual page on arrays? Your question is answered in the introductory paragraph.
  • yannis
    yannis almost 12 years
    @da_b0uncer Dictionary/map are common names for hash tables/associative arrays. A hash table is an implementation of an associative array, and simply put, is an array with hashed keys. A PHP array with ordered numerical indices is very close, if not exactly the same, with what you have in mind as an array, as when the indices are numerical the hash function is noop. But PHP arrays are so much more than mere "traditional arrays" ;)
  • K..
    K.. almost 12 years
    I learned that "associative arrays" are just a name give for datastructures, which look like arrays in a language, but aren't implemented as such (like in C).
  • phant0m
    phant0m almost 12 years
    Who cares? It doesn't even answer the question.
  • phant0m
    phant0m almost 12 years
    You are correct, but the question was about storing different type of values, not keys. Not sure why this was downvoted though.
  • Joris Timmermans
    Joris Timmermans almost 12 years
    @phant0m - I actually missed the title of the example completely, I was looking at the right-hand side of that example. Answer clarified, thank you.