Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the max key size for an array in PHP?

Tags:

arrays

php

key

I am generating associative arrays and the key value is a string concat of 1..n columns.

Is there a max length for keys that will come back to bite me? If so, I'll probably stop and do it differently.

like image 604
Ross Avatar asked Jan 21 '09 21:01

Ross


People also ask

How big can an array be in PHP?

There is no max on the limit of an array. There is a limit on the amount of memory your script can use. This can be changed in the 'memory_limit' in your php.

What is array key PHP?

The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys. Syntax: array array_keys($input_array, $search_value, $strict)

What are the 3 types of PHP arrays?

Create an Array in PHP In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.

What is a key in PHP?

The key() function is an inbuilt function in PHP which is used to return the index of the element of a given array to which the internal pointer is currently pointing.


1 Answers

It seems to be limited only by the script's memory limit.

A quick test got me a key of 128mb no problem:

ini_set('memory_limit', '1024M');  $key = str_repeat('x', 1024 * 1024 * 128);  $foo = array($key => $key);  echo strlen(key($foo)) . "<br>"; echo strlen($foo[$key]) . "<br>"; 
like image 182
Greg Avatar answered Sep 18 '22 02:09

Greg