note 110240 added to function.count
| From: | espertalhao04 at hotmail dot com | Date: | Thu, 04 Oct 2012 10:43:48 +0000 |
| Subject: | note 110240 added to function.count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-191126@lists.php.net to get a copy of this message | ||
jezed at email dot cz
you gave a great example, but you missed one particular thing: performance...
<?php
$data = array(
"apples" =>
array("red", "yellow", "pineapples"),
"bananas" =>
array("small", "medium", "big"),
"vegs" =>
array("potatoes", "carrots", "onions")
);
$rows = count($data,0);
$cols = (count($data,1)/count($data,0))-1;
print "There are {$rows} rows and {$cols} columns in the table!";
?>
is slower than
<?php
$data = array(
"apples" =>
array("red", "yellow", "pineapples"),
"bananas" =>
array("small", "medium", "big"),
"vegs" =>
array("potatoes", "carrots", "onions")
);
$cols = (count($data,1)/($rows = count($data,0)))-1;
print "There are {$rows} rows and {$cols} columns in the table!";
?>
in this way, you will get BOTH $cols and $rows and you reduced the code in 1 count...
in terms of performance with a 3x3 array, it's no problem, but a 50x50 array, it might hit
performance...
----
Server IP: 69.147.83.199
Probable Submitter: 95.136.43.99
----
Manual Page -- http://www.php.net/manual/en/function.count.php
Edit -- https://master.php.net/note/edit/110240
Del: integrated -- https://master.php.net/note/delete/110240/integrated
Del: useless -- https://master.php.net/note/delete/110240/useless
Del: bad code -- https://master.php.net/note/delete/110240/bad+code
Del: spam -- https://master.php.net/note/delete/110240/spam
Del: non-english -- https://master.php.net/note/delete/110240/non-english
Del: in docs -- https://master.php.net/note/delete/110240/in+docs
Del: other reasons-- https://master.php.net/note/delete/110240
Reject -- https://master.php.net/note/reject/110240
Search -- https://master.php.net/manage/user-notes.php