euel Posted January 3, 2012 Share Posted January 3, 2012 Gud Eve peeps! So i have 2 variable strings: $variable1 = item, item, item; $variable2 = number, number, number; I need to make this 2 variables into 1 array being variable 1 as a key value and variable 2 as a variable.. and i tried combining them using: $array = array_combine($variable1, $variable2); the problem is some item name are the same so it just overwrites the other so instead of giving me this: Array ([item] => number, [item] => number, [item =>number]) it gives me this: Array ([item =>number]) I really need them to be complete because i will loop each key and value to be inserted to a new row in my db.. thanks again for the help! Quote Link to comment https://forums.phpfreaks.com/topic/254262-array-combine/ Share on other sites More sharing options...
trq Posted January 3, 2012 Share Posted January 3, 2012 Arrays cannot contain duplicate keys. It really is that simple, and makes no sense any other way. Quote Link to comment https://forums.phpfreaks.com/topic/254262-array-combine/#findComment-1303634 Share on other sites More sharing options...
silkfire Posted January 3, 2012 Share Posted January 3, 2012 Maybe you need array_merge? array_combine creates a new array from keys and values. Quote Link to comment https://forums.phpfreaks.com/topic/254262-array-combine/#findComment-1303637 Share on other sites More sharing options...
AyKay47 Posted January 3, 2012 Share Posted January 3, 2012 the problem is some item name are the same so it just overwrites the other so instead of giving me this: Array ([item] => number, [item] => number, [item =>number]) the only way that a later key with the same value will not overwrite the previous key value is to have the keys be numeric, array_merge_recursive might be something to look at, but even so, as noted, it does not make sense to have duplicate keys in an array. Quote Link to comment https://forums.phpfreaks.com/topic/254262-array-combine/#findComment-1303669 Share on other sites More sharing options...
euel Posted January 3, 2012 Author Share Posted January 3, 2012 Thanks for the reply everyone! ill check and review my options again! The code i posted above wasn't really the code i have, it like this $branch = car - japan, car - usa, car - europe; $numbers = 1,2,3; $new = array_combine ($branch, $numbers); so its not really the same only the "car -" part... Quote Link to comment https://forums.phpfreaks.com/topic/254262-array-combine/#findComment-1303694 Share on other sites More sharing options...
trq Posted January 3, 2012 Share Posted January 3, 2012 That code is not valid php unless you have the constants car, japan, usa and europe defined somewhere and they represent numbers. Why not post your actual code. Quote Link to comment https://forums.phpfreaks.com/topic/254262-array-combine/#findComment-1303877 Share on other sites More sharing options...
euel Posted January 4, 2012 Author Share Posted January 4, 2012 Yay i solved it! Thanks for all the help! Quote Link to comment https://forums.phpfreaks.com/topic/254262-array-combine/#findComment-1303994 Share on other sites More sharing options...
AyKay47 Posted January 4, 2012 Share Posted January 4, 2012 Yay i solved it! Thanks for all the help! for this one i'm curious to see the final code. Quote Link to comment https://forums.phpfreaks.com/topic/254262-array-combine/#findComment-1303997 Share on other sites More sharing options...
euel Posted January 4, 2012 Author Share Posted January 4, 2012 sorry AyKay47 i wasn't able to explain how i solved it. Actually its more like a workaround rather than a solid solution. I knew from the beginning (b4 i started this topic) that variable with the same name when combined using the method i posted above will not work i just thought maybe there's an arguments of some sort for array_combine() which i do not know that can be added to make it work and it seems it doesn't have any. Since i really need them to be in array(key=>value) format, First i did some checking in each array if there are duplicate value then i appended each item/string with a (number) before using explode() then array_combine(). Right before inserting them in my db i did: foreach($new as $key => $values) { if(strpos($key, "(") != FALSE) { $key= substr($key, 0, strpos($key, "(")); } $query = "INSERT INTO table (key, value) VALUES ('{$key}', '{$value}' )"; $result = mysql_query($query, $connection); } to remove the appended string. Quote Link to comment https://forums.phpfreaks.com/topic/254262-array-combine/#findComment-1304047 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.