20

Is it possible in php to make an array an array key as well?

Example:

array(
   array('sample', 'abc') => 'sample value'
);
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Aldee
  • 4,439
  • 10
  • 48
  • 71
  • 12
    no, this is not possible. array keys must be integers or strings. this is explained in http://php.net/manual/en/language.types.array.php – Gordon Sep 05 '12 at 14:08
  • You could, theoretically, `serialize()` the array into a string to use as an array key, but I can't see why your design would require this and I don't actually advocate doing it. It would be fraught with problems, like having to reserialize every time you needed to change it. – Michael Berkowski Sep 05 '12 at 14:09
  • not only that it is not possible, but I don't even see a practical use for it - indexes and assoc keys in all programming languages (supporting them) to my knowledge are scalar values – Ivan Hušnjak Sep 05 '12 at 14:10
  • PHP array keys can be `integer` and `keys` only.. – Kalpesh Sep 05 '12 at 14:11
  • @IvanHušnjak compound key hashmaps would be one practical use for it. One can use `SplObjectStorage` for that though. – Gordon Sep 05 '12 at 14:11
  • I'm just absolutely DYING to know why you want to. Assuming it's not some horribly stray idea, I'm genuinely interested in how this is being used. – Sandy Gifford Sep 05 '12 at 14:12
  • You can have something like this instead: `array( 'sample'=>'abc', 'abc' => 'abc' );` – Kalpesh Sep 05 '12 at 14:12
  • @ccKep lua is not part of my knowledge :) – Ivan Hušnjak Sep 05 '12 at 14:15
  • 4 years ago, I was idiot :) – Aldee Apr 11 '16 at 01:13
  • 6 years later I admit I was thinking about this. I am passing an array to a complex function which will always return the same results for the same query. So I was thinking (for a few seconds) about caching the query along with its result, checking to see if the query was already performed, and returning the cached result rather than re-calculating work that was already done. There are better ways to do this ... but that's an application that came up. :) – TonyG Jul 02 '18 at 20:37
  • @TonyG, yeah. I totally agree. At some point, we find ourselves analyzing for wrong solutions and with enough experiences ahead, we then realized how silly that solution before. :-) – Aldee Jul 04 '18 at 03:53

4 Answers4

13

No, if you read the manual

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.

And :

The key can either be an integer or a string. The value can be of any type.

Kasia Gogolek
  • 3,374
  • 4
  • 33
  • 50
10

This is not possible - array keys must be strings or integers.

What you could do is use serialize:

$myArr = array( serialize(array('sample', 'abc')) => 'sample value');

Which will be the same as:

$myArr = array( 'a:2:{i:0;s:6:"sample";i:1;s:3:"abc";}' => 'sample value');

and could be accessed like:

echo $myArr[serialize(array('sample', 'abc'))];

But note that the serialised string which would be the unique identifier for the array item is clearly fairly complicated and almost impossible to type by hand.

Robin Winslow
  • 10,908
  • 8
  • 62
  • 91
1

PHP arrays can contain integer and string keys while since PHP does not distinguish between indexed and associative arrays. Look for php manual Php Manual

Damian SIlvera
  • 866
  • 1
  • 9
  • 19
-3

whats wrong with

array(
    'sample value' => array('sample', 'abc')
);

you could then do

foreach($array as $string => $child){
...
}

and use the $child for whatever purpose

nick
  • 2,743
  • 4
  • 31
  • 39