PHP Combining two arrays with same index key

Discussion in 'PHP' started by newgenservices, Jun 18, 2009.

  1. #1
    Here we have two arrays $array1 and $array2.

    $array1

    
    Array ( [0] => a [1] => b [2] => a [3] => b [4] => a [5] => b ) 
    
    Code (markup):
    $array2

    
    Array ( [0] => fruit1 [1] => flower1 [2] => fruit2 [3] => flower2 [4] => fruit3 [5] => flower3 ) 
    
    Code (markup):
    Now, i want an array with

    
    Array (
    [0] => Array ( [a] => fruit1 [b] => flower1),
    [1] => Array( [a] => fruit2 [b]=> flower2),
    [2] => Array( [a] => fruit3 [b] => flower3 ) 
    )
    
    Code (markup):
    Can someone please guide me on how to use the array_combine in this situation ?

    Thanks.
     
    newgenservices, Jun 18, 2009 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    I dont think array_combine can be used for this scenario, or may be because I never came into such thing.

    For your scenario, I have a custom function for you.

    
    $array1 = Array("a","b","a","b","a","b");
    $array2 = Array("fruit1","flower1","fruit2","flower2","fruit3","flower3");
    
    
    print_r(mycombine($array1,$array2));
    
    function mycombine($array1,$array2) {
    	$ret = Array();
    	$x=0;
    	foreach($array1 as $key => $index) {
    		if(!isset($ret[$x])) {
    			$ret[$x] = Array();
    		}
    		if(count($ret[$x]) ==2)
    			$x++;
    		$ret[$x][$index] = $array2[$key];
    	}
    
    	return $ret;
    }
    
    Code (markup):
    I hope it helps.
     
    Vooler, Jun 18, 2009 IP
    newgenservices likes this.
  3. Vbot

    Vbot Peon

    Messages:
    107
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This should work too
    $array1 = array ( 'a','b','a','b','a','b' );
    $array2 = array ( 'fruit1', 'flower1', 'fruit2', 'flower2', 'fruit3', 'flower3' );
    $array3 = array();
    for($i=0; $i<count($array1); $i+=2) {
    	$array3[] = array($array1[$i] => $array2[$i], $array1[$i+1] => $array2[$i+1]);
    }
    print_r($array3);
    PHP:
     
    Vbot, Jun 18, 2009 IP
    newgenservices likes this.
  4. newgenservices

    newgenservices Well-Known Member

    Messages:
    862
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    105
    Digital Goods:
    1
    #4
    Thanks Vooler and vbot for the simple function. Works like a charm :) +ve Repped you both.

    I had initially thought of getting the array into

    
    Array (
    [0] => Array ( [a] => fruit1 [b] => flower1),
    [1] => Array( [a] => fruit2 [b]=> flower2),
    [2] => Array( [a] => fruit3 [b] => flower3 ) 
    )
    
    Code (markup):
    as mentioned above as it was directly not possible in this situation to have

    
    Array (
     [a] => fruit1 [b] => flower1 
     [a] => fruit2 [b]=> flower2 
     [a] => fruit3 [b] => flower3
    )
    
    Code (markup):
    and i would end up having


    
    Array (
     [a] => fruit3 [b] => flower3
    )
    
    Code (markup):
    as arrays need unique indexes / keys.

    But later in the day after some careful thought, I have come up with this solution. Posting it here so it may be of some use to others who may encounter a similar situation.

    
    <?php
    
    $array1 = Array("a","b","a","b","a","b");
    $array2 = Array("fruit1","flower1","fruit2","flower2","fruit3","flower3");
    
    
    $fruits_array = array(); 	reset($fruits_array);
    $flowers_array = array();	reset($flowers_array);
    
    while (($val1 = each($array1)) && ($val2 = each($array2))) {
    
    	if (preg_match("/a/i", $val1['value'])){
    		$fruit_value = $val2['value'];
    		array_push($fruits_array,$fruit_value);	
    	}elseif (preg_match("/b/i", $val1['value'])){
    		$flower_value = $val2['value'];		
    		array_push($flowers_array,$flower_value);
    	}														
    
    } 
    
    $array3 = array_combine($fruits_array,$flowers_array);
    
    print_r($array3);
    
    ?>
    
    Code (markup):
     
    newgenservices, Jun 18, 2009 IP