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)
PHP 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
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
You can use Array Count function count or sizeof function.
try below code:
$array = array(
1,
2,
3,
["thing1", "thing2", "thing3"]
);
echo count($array[3]); //out put 3
echo sizeof($array[3]); //out put 3
You can use the count function:
echo count($array[3]);
However if the array you want to get the length is not always in this same position you could do something like this:
foreach ($array as $element) {
if (is_array($element) {
echo count($element);
}
}
Nice problem, here is a solution I stole from the PHP Manual:
function countdim($array)
{
if (is_array(reset($array)))
{
$return = countdim(reset($array)) + 1;
}
else
{
$return = 1;
}
return $return;
}
you can try this:
$a["one"]["two"]["three"]="1";
function count_dimension($Array, $count = 0) {
if(is_array($Array)) {
return count_dimension(current($Array), ++$count);
} else {
return $count;
}
}
print count_dimension($a);
What you need to do is first create a list of strings matching the length and then pick a random one from them. So loop through all of the strings, check the length and add to $match, then use array_rand() to pick one of the matching ones...
function getName($random_num)
{
$names = array
(
'Juans',
'Luisss',
'Pedroaa',
'testNames1',
'testNames2',
'testName3',
'test', //This should not be return because im only generating a number random of 5-10 and this character has only 4 character length
'tse', //This should not be return because im only generating a number random of 5-10 and this character has only 3 character length
// and so on
);
$randomString = "";
$match = [];
foreach ( $names as $name )
{
$length_string = strlen($name);
if ($random_num == $length_string)
{
$match[] = $name;
}
}
// If non found return ''
return !empty($match) ? $match[array_rand($match)] : '';
}
You can try the below code. You was adding the below line in the loop that was wrong.
$value = rand(5,10);
Now, This program will return the name that will be equal to the generated random number that is between 5 to 10. Also it will return the message if no name exist according to the random number. For example: 8
<?php
function getName($random_num)
{
$names = array
(
'Juans',
'Luisss',
'Pedroaa',
'testNames1',
'testNames2',
'testName3',
'test', //This should not be return because im only generating a number random of 5-10 and this character has only 4 character length
'tse', //This should not be return because im only generating a number random of 5-10 and this character has only 3 character length
// and so on
);
$randomString = "";
for ($i = 0; $i <count($names); $i++)
{
$length_string = strlen($names[$i]);
if ($random_num == $length_string)
{
return "name:".$names[$i].$random_num;
}
}
}
$random_num = rand(5,10);
$returned_name = getName($random_num);
if ($returned_name == "")
{
echo "No Name Exist having length: " . $random_num;
}
else
{
echo $returned_name;
}
?>