note 124802 added to function.count

From: Date: Sun, 08 Mar 2020 00:12:29 +0000
Subject: note 124802 added to function.count
Groups: php.notes 
Request: Send a blank email to php-notes+get-214297@lists.php.net to get a copy of this message
In special situations you might only want to count the first level of the array to figure out how many entries you have, when they have N more key-value-pairs. <?php $data = [ 'a' => [ 'bla1' => [ 0 => 'asdf', 1 => 'asdf', 2 => 'asdf', ], 'bla2' => [ 0 => 'asdf', 1 => 'asdf', 2 => 'asdf', ], 'bla3' => [ 0 => 'asdf', 1 => 'asdf', 2 => 'asdf', ], 'bla4' => [ 0 => 'asdf', 1 => 'asdf', 2 => 'asdf', ], ], 'b' => [ 'bla1' => [ 0 => 'asdf', 1 => 'asdf', 2 => 'asdf', ], 'bla2' => [ 0 => 'asdf', 1 => 'asdf', 2 => 'asdf', ], ], 'c' => [ 'bla1' => [ 0 => 'asdf', 1 => 'asdf', 2 => 'asdf', ] ] ]; $count = array_sum(array_values(array_map('count', $data))); // will return int(7) var_dump($count); // will return 31 var_dump(count($data, 1)); ?> ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.195 (proxied: 2003:ed:e735:e200:2de0:547e:f87f:3b3c) ---- Manual Page -- http://php.net/manual/en/function.count.php Edit -- https://master.php.net/note/edit/124802 Del: integrated -- https://master.php.net/note/delete/124802/integrated Del: useless -- https://master.php.net/note/delete/124802/useless Del: bad code -- https://master.php.net/note/delete/124802/bad+code Del: spam -- https://master.php.net/note/delete/124802/spam Del: non-english -- https://master.php.net/note/delete/124802/non-english Del: in docs -- https://master.php.net/note/delete/124802/in+docs Del: other reasons-- https://master.php.net/note/delete/124802 Reject -- https://master.php.net/note/reject/124802 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#214297) next »