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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: szfjozsef at gmail dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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 Dec 21 18:01:29 2024 UTC