LMSouq
database Open

PHP: merge two arrays while keeping keys instead of reindexing?

GA
Garrett
1 month ago
3 views
Problem Description
How can I merge two arrays (one with string => value pairs and another with int => value pairs) while keeping the string/int keys? None of them will ever overlap (because one has only strings and the other has only integers). Here is my current code (which doesn't work, because array_merge is re-indexing the array with integer keys): // get all id vars by combining the static and dynamic $staticIdentifications = array( Users::userID => "USERID", Users::username => "USERNAME" ); // get the dynamic vars, formatted: varID => varName $companyVarIdentifications = CompanyVars::getIdentificationVarsFriendly($_SESSION['companyID']); // merge the static and dynamic vars (*** BUT KEEP THE INT INDICES ***) $idVars = array_merge($staticIdentifications, $companyVarIdentifications);

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base