array merger but data become unique

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
codefreak
Forum Newbie
Posts: 7
Joined: Sun Mar 04, 2012 8:07 pm

array merger but data become unique

Post 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. :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: array merger but data become unique

Post 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
(#10850)
codefreak
Forum Newbie
Posts: 7
Joined: Sun Mar 04, 2012 8:07 pm

Re: array merger but data become unique

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: array merger but data become unique

Post by Celauran »

You've got duplicate keys and duplicate values, which is going to be problematic.
codefreak
Forum Newbie
Posts: 7
Joined: Sun Mar 04, 2012 8:07 pm

Re: array merger but data become unique

Post 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
Post Reply