try
$maxlen = max(array_map('strlen', $ary));
Answer from user187291 on Stack OverflowPHP array: count or sizeof? - Stack Overflow
php - Get string length of each element in an array - Stack Overflow
How to declare the length/size of an array in php, so users can input any size - Stack Overflow
php - Length array in array - Stack Overflow
What is the primary function to get the PHP array length?
How do I properly use the PHP array length in a for loop?
";}
How does checking the PHP array length affect performance?
Videos
try
$maxlen = max(array_map('strlen', $ary));
Sure:
function getmax($array,
curmax) {
return $cur >= count($array) ? $curmax :
getmax($array, $cur + 1, strlen($array[$cur]) > strlen($array[$curmax])
?
curmax);
}
$index_of_longest = getmax($my_array, 0, 0);
No loop there. ;-)
I would use count() if they are the same, as in my experience it is more common, and therefore will cause less developers reading your code to say "sizeof(), what is that?" and having to consult the documentation.
I think it means sizeof() does not work like it does in C (calculating the size of a datatype). It probably made this mention explicitly because PHP is written in C, and provides a lot of identically named wrappers for C functions (strlen(), printf(), etc)
According to phpbench:
Is it worth the effort to calculate the length of the loop in advance?
Copy//pre-calculate the size of array
$size = count($x); //or $size = sizeOf($x);
for ($i=0; $i<$size; $i++) {
//...
}
//don't pre-calculate
for ($i=0; $i<count($x); $i++) { //or $i<sizeOf($x);
//...
}
A loop with 1000 keys with 1 byte values are given.
| count() | sizeof() | |
|---|---|---|
| With precalc | 152 | 212 |
| Without precalc | 70401 | 50644 |
(time in µs)
So I personally prefer to use count() instead of sizeof() with pre calc.
You need to access the current array item like $somePosts[$i] and not $somePosts
for($i = 0, $size = count($somePosts); $i < $size; ++$i) {
if (strlen(utf8_decode($somePosts[$i])) > $max_length) {
$offset = ($max_length - 3) - strlen($somePosts[$i]);
$somePosts[$i] = substr($somePosts[$i], 0, strrpos($reviewPosts, ' ', $offset)) . '...';
}
}
As alternative you can use array_map
<?php
$strs = array('abcdefgh', 'defghjklmn', 'ghjefggezgzgezg');
$max = 5;
$strs = array_map(function($val) use ($max) {
if (strlen($val.'') < $max) return $val.'';
return substr($val.'', 0,$max-3).'...';
},$strs);
var_dump($strs);
EDIT implicit casts added to cast object to string
In PHP you define/declare an array by just
$oldSchool = array();
$newSchool = []; // use this one!
You can add new elements to it by simply using [] next to the array as:
$array[] = 'new element';
count($array); // returns the lenght of the array
You can easily iterate an array using a foreach loop:
foreach($array as $key => $value) {
echo sprintf('key: %s, value: %s',$key, $value) . PHP_EOL;
}
There are plenty of in-build php functions to play with arrays https://www.php.net/manual/en/ref.array.php
PHP doesn't have a concept of arrays. It has ordered maps which it calls arrays. They have no finite size. You can't have a fixed size array unless you use the SplFixedArray class
To create PHP's "array" you can use the following syntax.
$myArray = [];
To add elements you use keys. This will map one value to another.
$myArray['myKey'] = 'My string';
You can also use numerical keys or append a new value to the map.
$myArray[42] = 'My string of 42';
$myArray[] = 'Appended value'; // the key will be generated by PHP and will be the next available numerical value