Home > Backend Development > PHP Tutorial > PHP counts the number of cells or object attributes in an array

PHP counts the number of cells or object attributes in an array

PHPz
Release: 2024-03-21 16:12:01
forward
1101 people have browsed it

php editor Strawberry introduces you how to calculate the number of units or object attributes in a PHP array. In PHP, you can use the count() function to get the number of units in an array, or you can use the count() or sizeof() function to get the number of attributes of an object. These functions can help you quickly and accurately count the number of elements in an array or object, making programming more efficient and easier. Next, let’s learn in detail how to implement these functions in PHP.

Calculate the number of cells or object attributes in the array

Array

  • count($array): Count the number of cells in the array, including cells in nested arrays.
<?php
$array = array(1, 2, 3, array(4, 5, 6));
echo count($array); // Output: 4
?>
Copy after login
  • sizeof($array): Equivalent to count().
<?php
$array = array(1, 2, 3, array(4, 5, 6));
echo sizeof($array); // Output: 4
?>
Copy after login

Object

The number of object attributes can be calculated by the following method:

  • count($object): Count the number of all public attributes of the object.
<?php
class Person {
public $name;
public $age;
}

$person = new Person();
$person->name = "John Doe";
$person->age = 30;
echo count($person); // Output: 2
?>
Copy after login
  • get_object_vars($object): Returns an associative array containing all public attributes of the object. The key of the array is the attribute name and the value is the attribute value.
<?php
class Person {
public $name;
public $age;
}

$person = new Person();
$person->name = "John Doe";
$person->age = 30;
$properties = get_object_vars($person);
echo count($properties); // Output: 2
?>
Copy after login
  • array_keys($object): Returns an array containing all public property names of the object.
<?php
class Person {
public $name;
public $age;
}

$person = new Person();
$person->name = "John Doe";
$person->age = 30;
$propertyNames = array_keys(get_object_vars($person));
echo count($propertyNames); // Output: 2
?>
Copy after login

Nested Arrays and Objects

If an array or object contains other arrays or objects, you can use the recursive method to calculate the total number of its cells or properties.

<?php
function countNestedArray($array) {
$count = 0;
foreach ($array as $value) {
if (is_array($value)) {
$count = countNestedArray($value);
} else {
$count ;
}
}
return $count;
}
?>
Copy after login
<?php
class Person {
public $name;
public $age;
public $children;
}

function countNestedObject($object) {
$count = 0;
foreach ($object as $property => $value) {
if (is_object($value)) {
$count = countNestedObject($value);
} else {
$count ;
}
}
return $count;
}
?>
Copy after login

The above is the detailed content of PHP counts the number of cells or object attributes in an array. For more information, please follow other related articles on the PHP Chinese website!

source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template