php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80746 shuffle really should maintain key=>value relation
Submitted: 2021-02-13 16:17 UTC Modified: 2021-11-24 12:25 UTC
From: szfjozsef at gmail dot com Assigned:
Status: Open Package: *General Issues
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
42 + 14 = ?
Subscribe to this entry?

 
 [2021-02-13 16:17 UTC] szfjozsef at gmail dot com
Description:
------------
---
From manual page: https://php.net/function.shuffle
---
People around the world are struggling by using the shuffle() function. There is absolutely no reason to reset the keys to 0,1,2,3 when shuffling the array.
Please make shuffle() maintain the keys.

Expected result:
----------------
call shuffle() and move on.

Actual result:
--------------
Have to write cumbersome, long, inefficient functions just to restore the key=>value associations.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-11-22 17:58 UTC] cmb@php.net
> People around the world are struggling by using the shuffle()
> function.

Citation needed. :)

> Have to write cumbersome, long, inefficient functions just to
> restore the key=>value associations.

<https://www.php.net/manual/en/function.shuffle.php#94697> does
neither look cumbersome nor long – but might be inefficient.
 [2021-11-24 12:25 UTC] szfjozsef at gmail dot com
> Citation needed. :)

Well, this is not really possible, but if you accept as citation, please make a Google search, this reveals how many people actually wrote some kind of function to restore the original key-value relation.

Yes, it is a long and inefficient function... A lot of processor time is needed to do something, that in the first place already is available in the shuffle() internal function.

I strongly think, not maintaining the key=>value associations (in all other PHP functions also) is a waste of time and resources. There is no reason to do this, since there are no benefits of this.

There is room for a second parameter, eg. $maintainKeys = false. Simple and clean, and it does not break old code. Of course, for all functions that destroy the keys when processing arrays...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 16:01:29 2024 UTC