|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-04-18 12:56 UTC] anjvelu at gmail dot com
Description:
------------
i need a feature through array.
Test script:
---------------
function getComments(){
return array(
"id" => 1,
"comment" => "this is test comment",
"favorite_users" => array(10, 15, 2, 5),
"favorite_users_count" => count(array(10, 15, 2, 5))
);
}
Expected result:
----------------
---
From manual page: https://php.net/language.types.array
---
i need a feature through array.
function getComments(){
return array(
"id" => 1,
"comment" => "this is test comment",
"favorite_users" => array(10, 15, 2, 5),
"favorite_users_count" => count(array(10, 15, 2, 5))
);
}
The above function, there is key "favorite_users" having values and the key "favorite_users_count" only holds count of array key "favorite_users".
Here the key "favorite_users_count" again setting the values for count array.
Is there any way to achieve the count calling the array key "favorite_users".
Thanks
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 14:00:01 2025 UTC |
what's wrong with doing this yourself? function getComments(){ $comments = [ "id" => 1, "comment" => "this is test comment", "favorite_users" => [10, 15, 2, 5], ]; return $comments + ['favorite_users_count' => count($comments['favorite_users'])]; } See https://3v4l.org/Xmv2Y