php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #77918 i need a feature through array.
Submitted: 2019-04-18 12:56 UTC Modified: 2019-04-18 15:59 UTC
From: anjvelu at gmail dot com Assigned: girgias (profile)
Status: Not a bug Package: Unknown/Other Function
PHP Version: Next Major Version OS:
Private report: No CVE-ID: None
 [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



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-04-18 13:07 UTC] girgias@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: girgias
 [2019-04-18 13:07 UTC] girgias@php.net
Just have a variable which holds ``array(10, 15, 2, 5)`` and you can reference this variable in the count function.
 [2019-04-18 13:08 UTC] sjon at hortensius dot net
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
 [2019-04-18 15:59 UTC] requinix@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 14 17:01:33 2024 UTC