php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80522 Conflict in array name with implode
Submitted: 2020-12-15 22:29 UTC Modified: 2020-12-16 00:07 UTC
From: pl dot lamballais at flashover dot fr Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.3.25 OS: Lunix Debian
Private report: No CVE-ID: None
 [2020-12-15 22:29 UTC] pl dot lamballais at flashover dot fr
Description:
------------
1) declare an array
2) use this array as return from a function
3) fill this array manually with values
4) implode the array
The first and second item are swapped in the result string

Test script:
---------------
$tab_ret = array();
$tab_ret = myfunction($param);
$data1 = $tab_ret[0];
$data2 = $tab_ret[1];

$tab_ret[0] = "Marcus";
$tab_ret[1] = "John";
$tab_ret[2] = "Peter";
$tab_ret[3] = "Paul";	
$tab_ret[4] = "Ringo";	
$str = implode("-",$tab_ret);
// Result string is John-Marcus-Peter-Paul-Ringo
If rather than using $tab_ret as return from the function you use another array, that's OK.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-12-16 00:07 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2020-12-16 00:07 UTC] requinix@php.net
function myfunction($param) {
    $tab_ret = array();
    $tab_ret[1] = "One";
    $tab_ret[0] = "Zero";
    return $tab_ret;
}

implode joins in the order the elements were added to the array - not in order according to their keys.
Fix the function to create [0] before [1], or ksort() the array, or write code to build the string exactly as you want it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 13:01:30 2024 UTC