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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: oliver at openbrackets dot net
New email:
PHP Version: OS:

 

 [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: Fri Jun 14 13:01:30 2024 UTC