php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #75640 Legacy compat function for deprecated each()
Submitted: 2017-12-06 13:37 UTC Modified: 2019-04-26 03:27 UTC
From: oliver at openbrackets dot net Assigned:
Status: Wont fix Package: Documentation problem
PHP Version: 7.2.0 OS: Any
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:
4 - 4 = ?
Subscribe to this entry?

 
 [2017-12-06 13:37 UTC] oliver at openbrackets dot net
Description:
------------
Tried to put this in user notes, but that system doesn't appear to work. 

I fully agree with deprecating each(). However there are lots of old third party libraries out there which use it in arcane ways. 

Refactoring third party code is not fun. So suggest to provide a workaround in the docs to keep these working. 

Suggest to put a "leach() = Legacy each()" function in docs, with code similar to that in "test script" field.

Test script:
---------------
<?php


/**                                                                                                                                                           
 * leach() = "legacy each"                                                                                                                                    
 *                                                                                                                                                            
 * replacement for deprecated each() for php7.2                                                                                                               
 * use this if refactoring would be too painful and performance is not relevant                                                                               
 *                                                                                                                                                            
 */
function leach(&$arr)
{
  $k = key($arr);
  if ($k === null)
  {
    // https://secure.php.net/manual/en/function.each.php                                                                                                     
    // If the internal pointer for the array points past the end of the array contents, each() returns FALSE.                                                 
    return false;
  }
  else
  {
    // https://secure.php.net/manual/en/function.each.php                                                                                                     
    // Returns the current key and value pair from the array                                                                                                  
    // array. This pair is returned in a four-element array, with the                                                                                         
    // keys 0, 1, key, and value. Elements 0 and key contain the key                                                                                          
    // name of the array element, and 1 and value contain the data.                                                                                           
    $v = current($arr);
    $retval = [
      1 => $v,
      'value' => $v,
      0 => $k,
      'key' => $k
    ];
    // https://secure.php.net/manual/en/function.each.php                                                                                                     
    // After each() has executed, the array cursor will be left on the                                                                                        
    // next element of the array, or past the last element if it hits the                                                                                     
    // end of the array. You have to use reset() if you want to traverse                                                                                      
    // the array again using each.                                                                                                                            
    next($arr);
    return $retval;
  }
}

?>

Expected result:
----------------
docs for each() revised


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-04-26 03:27 UTC] peehaa@php.net
-Status: Open +Status: Wont fix
 [2019-04-26 03:27 UTC] peehaa@php.net
Not sure I agree the docs are the best platform for this.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 03 17:01:33 2024 UTC