0
$export = db::get($data);

foreach ($export as $user) {
    //
}

echo "Totol User: " . $export->count();

Hello, I have a problem. I'm pulling array() with db::get($data). Then I want to show the total number of records with a function such as $export->count().

Is there an example suitable for this structure? Can you help me?

2
  • count() in the documentation. Commented Feb 20, 2019 at 19:32
  • Probably overkill, but you could return an ArrayObject from get() or do $export = new ArrayObject(db::get($data)); and then $export->count;. Commented Feb 20, 2019 at 20:01

3 Answers 3

2

There's not much to it, but there seems to be a misunderstanding about what an array can do.

An array in PHP is not an object with methods like ->count(). It has no methods or properties.

Instead you pass the array as an argument to the built-in PHP function count().

echo "Totol User: " . count($export);

The sizeof() function is an alias of count(). They are interchangeable.

Sign up to request clarification or add additional context in comments.

Comments

0

You can also use sizeof():

echo "Total number of users: " . sizeof($export);

Check out w3schools.com fro more info: https://www.w3schools.com/php/func_array_sizeof.asp

Also if db is a variable, the it should be $db not simply db

2 Comments

db::get($data) is static class function
Okay my mistake, should be Db if you want to follow best practice.
0

I don't know what is db::get($data) function but if it's return an array, you can count number of items with sizeof function:

sizeof($export);

Source: http://php.net/manual/en/function.sizeof.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.