Current location: Hot Scripts Forums » Programming Languages » PHP » Merging 2 array with one as Keys and Values


Merging 2 array with one as Keys and Values

Reply
  #1 (permalink)  
Old 05-18-09, 05:40 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Merging 2 array with one as Keys and Values

How do I actually combine 2 array into one and have one array assigned being the key and another array being the value.

This is how the 2 array look like.

PHP Code:

// Weight

Array
(
    [
0] => 0.15
    
[1] => 1.05
    
[2] => 1.05
    
[3] => 1.05
    
[4] => 1.05
    
[5] => 1.05
    
[6] => 1.05
    
[7] => 1.05
    
[8] => 1.05
    
[9] => 0.45
    
[10] => 1.05

)
// Name
Array
(
    [
0] => Li
    
[1] => Keo
    
[2] => Tan
    
[3] => Baj
    
[4] => Koh
    
[5] => Jack
    
[6] => Yeap
    
[7] => Dan
    
[8] => Blair
    
[9] => Ninny
    
[10] => Kenny

And change to.

PHP Code:

Array

(
    [
Name] => [Weight]

Thanks.
Reply With Quote
  #2 (permalink)  
Old 05-18-09, 05:57 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
I have found the answer. There's this function that does the trick.

PHP Code:

/*
    Name:         array_merge_assoc
    Author:     Markus Diersbock
    Details:     Takes two related indexed arrays and
            combines them into one associative array.
            
    Notes:        If $keyarray is NOT greater or equal to $valuearray
            the function will error unless the optional $force=TRUE.

    Revisions:    2003/12/13 - Created
            2004/01/14 - Added code for unequal arrays
*/


function array_merge_assoc($keyarray$valuearray$force=FALSE){
    
$i=0;

    if ((
count($keyarray) >= count($valuearray)) || $force==TRUE) {
        foreach(
$keyarray as $element){
            
$aryreturn[$element]=$valuearray[$i++];
        }
    } else {
        return 
'Arrays are not of equal size';
    }

    return 
$aryreturn;

Reply With Quote
  #3 (permalink)  
Old 05-18-09, 06:16 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Even easier:
PHP Code:

$array array_combine($names$weights); 

PHP: array_combine - Manual
Reply With Quote
  #4 (permalink)  
Old 05-18-09, 07:12 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
I know. But if I use array_combine, it won't return a full list. It will just return array containing 0.15,0.45 and 1.05.

Ok, now I have this:

PHP Code:

Array
(
    [
Li] => 0.15
    
[Keo] => 1.05
    
[Tan] => 1.05
    
[Baj] => 1.05
    
[Koh] => 1.05
    
[Jack] => 1.05
    
[Yeap] => 1.05
    
[Dan] => 1.05
    
[Blair] => 1.05
    
[Ninny] => 0.45
    
[Kenny] => 1.05

And this particular function:

PHP Code:

// Took this code from php.net
function unique_events($array){
    
//checks $array for duplicate values and returns an
        //array containing the keys of duplicates
   // It was supposedly to be array_intersect_assoc, however, it doesn't return me any result.
   // So I changed to array_intersect.
    
$countarray_intersect($arrayarray_fliparray_count_values($array)));
    
print_r($count);
    foreach(
$array as $key=>$value){
        if (
in_array($value,$count)){
            
$return[$value][]=$key;
        }
    }
    return 
$return;

And it works fine and gave me the result I wanted. But I notice that it always return me one less value from the original array. The result as below:

PHP Code:

Array
(
    [
1.05] => Array
        (
            [
0] => Keo
            
[1] => Tan
            
[2] => Baj
            
[3] => Koh
            
[4] => Jack
            
[5] => Yeap
            
[6] => Dan
            
[7] => Blair
            
[8] => Kenny
        
)

    [
0.45] => Array
        (
            [
0] => Ninny
        
)


As you can see, it's missing of one value. Which should also include.

PHP Code:

Array
(
    [
0.15] => Array
        (
            [
0] => Li
        
)

Why is it so?
Reply With Quote
  #5 (permalink)  
Old 05-18-09, 07:23 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Again, I have found the solution from php.net. Arghh... It's always after I posted and then I found the answer.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to converet keys to values without foreach statement ginasuit PHP 2 02-19-09 10:51 AM
Searching a array with wildcards phpdoctor PHP 12 02-20-08 03:35 AM
Implode Array keys Jay6390 PHP 6 12-23-07 05:21 PM
Merging results from two mysql databases Deansatch PHP 65 03-02-07 06:12 PM
Drop Down menu order peterc PHP 1 08-20-04 02:23 AM


All times are GMT -5. The time now is 08:36 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.