Jump to content

Array Combine?


euel

Recommended Posts

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!

 

 

Link to comment
https://forums.phpfreaks.com/topic/254262-array-combine/
Share on other sites

 

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.

Link to comment
https://forums.phpfreaks.com/topic/254262-array-combine/#findComment-1303669
Share on other sites

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...

Link to comment
https://forums.phpfreaks.com/topic/254262-array-combine/#findComment-1303694
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254262-array-combine/#findComment-1304047
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.