What is the max key size for an array in PHP?
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>";
PHP associative array's keys (indexes) limitations?
What is the max key size for an array in PHP?
This question is almost the exact same. But if you dont want to trust anything unofficial, just stick to using less small keys. You may even get some performance benefits out of it.
EDIT: And as the The PHP Manual says:
Note: It is no problem for a string to become very large. PHP imposes no boundary on the size of a string; the only limit is the available memory of the computer on which PHP is running..
Search for highest key/index in an array
This should work fine
$arr = array( 1 => "A", 10 => "B", 5 => "C" );
max(array_keys($arr));
Maximum number of dimensions in a PHP array
Each array
level generally costs you 304
bytes (determined by checking memory usage, creating an array, then checking again), and the total amount of memory can be calculated using ini_get("memory_limit")
. To get the current usage, run memory_get_usage()
.
On my computer:
ini_get("memory_limit")
returns"128M"
, or134217728
bytes.memory_get_usage()
base use is627120
so I would expect the limit on my kit to be 439442
depth
Php associative array sort and get key with highest length
You can use array_keys
and pass a second parameter to filter the returned keys to only include the max
ones. You can then find the longest key by using array_reduce
and a function that checks the lengths of the strings and throws out the shortest one, like so:
$array = array(
'abc' => 10,
'def' => 8,
'fff' => 3,
'abcr' => 10,
'adsfefs' => 10
);
$keys = array_keys($array, max($array));
$longestKey = array_reduce($keys, function ($a, $b) { return strlen($a) > strlen($b) ? $a : $b; });
var_dump($longestKey);
Be aware that if there are two or more strings that are the same length, it will return the last one.
What's the maximum key length for PHP APC?
I really don't know PHP, but it looks like values are added using apc_add()
or apc_store()
, which take keys which are strings. I would infer that the maximum key length is therefore the maximum length of a string in PHP.
According to PHP: Strings,
Note: It is no problem for a string to become very large. PHP imposes no boundary on the size of a string; the only limit is the available memory of the computer on which PHP is running.
Getting an array key using the max() function
array_search function would help you.
$returnThis = array_search(max($arrCompare),$arrCompare);
maximum value in php array for each key value
First, rotate array to makes columns as lines
$arr1= array(
0=>array(1,2,3),
1=>array(2,4,6),
2=>array(25,4,5)
);
$arr2 = array();
foreach($arr1 as $subArray) {
foreach($subArray as $k=>$v) {
$arr2[$k][] = $v;
}
}
Then, use array_map as @HamZa did
$max_array = array_map('max', $arr2);
print_r($max_array);
Related Topics
Finding the PHP File (At Run Time) Where a Class Was Defined
How to Export a SQL Table Without Access to the Server or PHPmyadmin
Notice: Unknown: Skipping Numeric Key 1 in Unknown on Line 0
Submitting JSON Data via Jquery Ajax.Post to PHP
Calling a Particular PHP Function on Form Submit
Disabling Strict Standards in PHP 5.4
What Does Double Question Mark () Operator Mean in PHP
PHP Carbon, Get All Dates Between Date Range
Laravel 5 Not Finding CSS Files
PHP Upload, Extract and Progressbar
Calculate Skip Value for Given Record for Sorted Paging