Bug #80522 [Opn->Nab]: Conflict in array name with implode
| From: | requinix@php.net | Date: | Wed, 16 Dec 2020 00:07:24 +0000 |
| Subject: | Bug #80522 [Opn->Nab]: Conflict in array name with implode | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-231109@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80522&edit=1
ID: 80522
Updated by: requinix@php.net
Reported by: pl dot lamballais at flashover dot fr
Summary: Conflict in array name with implode
-Status: Open
+Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: Lunix Debian
PHP Version: 7.3.25
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2020-12-15 22:29:17] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80522&edit=1