[php-src] Issue #12429: array_map generates wrong indexes when used inside a Foreach

From: Date: Thu, 12 Oct 2023 18:53:16 +0000
Subject: [php-src] Issue #12429: array_map generates wrong indexes when used inside a Foreach
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-245555@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/12429 Author: xonipatino ### Description I have the following code: ``` $arrAdjuntoEntity = $adjuntoModel ->join('adjunto_usuario', 'adjunto.id = adjunto_usuario.id_adjunto') ->where('adjunto_usuario.id_usuario', 1) ->findAll(); $arrTipoAdjuntoUsuario = db_connect() ->table('tipo_adjunto_usuario') ->get() ->getResult(); $arrAdjuntoUsuario = []; foreach ($arrTipoAdjuntoUsuario as $tipoAdjuntoUsuario) { $arrAdjuntoEntityTipo = array_filter( $arrAdjuntoEntity, static fn($adjuntoEntity): bool => ($adjuntoEntity->id_tipo_adjunto_usuario == $tipoAdjuntoUsuario->id) ); $arrAdjuntoUsuario[$tipoAdjuntoUsuario->id] = array_map( static fn($adjuntoEntity) => [$adjuntoEntity->nombre, $adjuntoEntity->tamano], $arrAdjuntoEntityTipo ); } ``` when I print the variable json_encode($arrAdjuntoUsuario) print the following: ``` { "1": [], "2": [ [ "cedula.pdf", 1252056 ] ], "3": [], "4": [], "5": { "1": [ "procuraduria.pdf", 1252056 ] }, "6": { "2": [ "procuraduria.pdf", 1252056 ] } } ``` The following items have an error, since they add property "1" and "2" within the array of item 5 and 6, those indexes should not exist. ``` "5": { "1": [ <-- error, unknown index "procuraduria.pdf", 1252056 ] }, "6": { "2": [ <-- error, unknown index "procuraduria.pdf", 1252056 ] } } ``` The result should print the same as item 2 ``` "2": [ [ "cedula.pdf", 1252056 ] ], ``` that is, it should print like this: ``` "5": [ [ "procuraduria.pdf", 1252056 ] ], "6": [ [ "procuraduria.pdf", 1252056 ] ] } ``` ### PHP Version PHP 8.2.11 ### Operating System Windows

« previous php.bugs (#245555) next »