Page 1 of 1

array merger but data become unique

Posted: Mon Mar 12, 2012 8:16 pm
by codefreak
Hello all I have a problem,

I have two array:
arr1=array('red', 'red', 'snake', 'blue');
arr2=array('color', 'color', 'animal', 'color');
and I want arr3=array('red'=>'color', 'snake'=>'animal', 'blue'=>'color' );

look the red fill can be pink fill, because index and value has same but if same value not merge like blue=>color because different index and or different value.
And the sequence of final result is consecutive like begin red=>color and end by blue=>color.

Anyone would to like to help me, what PHP function can do that?
Thank you very much. :)

Re: array merger but data become unique

Posted: Mon Mar 12, 2012 11:55 pm
by Christopher
Have you checked the manual? One glance at the array section of the manual and the third function seems to be what you want:

http://us3.php.net/manual/en/book.array.php

Re: array merger but data become unique

Posted: Tue Mar 13, 2012 1:18 am
by codefreak
Christopher wrote:Have you checked the manual? One glance at the array section of the manual and the third function seems to be what you want:

http://us3.php.net/manual/en/book.array.php
Hi Christopher,
Thank you for reply.
YEs, i do. Before I write In here, I has search in many blog and manual in PHP.net but not yet found.
I have found the similar ex: array_combine but in this function the same index become merge. For example:

Code: Select all

$arr1=array('red', 'red', 'snake', 'blue');
$arr2=array('color', 'color', 'animal', 'color');
print_r(array_combine($arr2, $arr1));
The output will:

Code: Select all

Array ( [color] => blue [animal] => snake )
but
I want the output : Array ( [color] => red [animal] => snake [color] => blue )
Would you like to help me to give an clue for this problem?
Thank you Christopher.

Re: array merger but data become unique

Posted: Tue Mar 13, 2012 5:50 am
by Celauran
You've got duplicate keys and duplicate values, which is going to be problematic.

Re: array merger but data become unique

Posted: Tue Mar 13, 2012 6:54 am
by codefreak
Yeah !!!
I have solve my problem....
I was make a function and not use built-in function PHP...
:offtopic:

Thank you all. :D