My aim to create multiple possible of out comes with 3 arrays.

I achieved it but i got struck with one problem.

I find there is repeated values with different keys.

below is the code i tried to generate .

$top = array(array('toplinner_name' => M1), array('toplinner_name' => M2));
$flute1 = array(array('f1_name' => M3));
$single_array = compares($top, $flute1); //combaining arrays
$bottom = array(array('bottomlinner_name' => M1), array('bottomlinner_name' => M2));
$single_array = compares($single_array, $bottom); //combaining arrays

function compares($a, $b) {
    if (is_array($a) && is_array($b)) {
        if (count($a) < count($b)) {
            list($a, $b) = array($b, $a);
        }
        $new = array();
        $k = 0;
        for ($i = 0; $i < count($b); $i++) {
            for ($j = 0; $j < count($a); $j++) {
                $new[$k] = array_merge($b[$i], $a[$j]);
                $k++;
            }
        }
        return $new;
    }
    return null;
}

with the above code i am getting out put as

Array{
     '0'=>{'toplinner_name'=>'M1','f1_name'=>'M3','bottomlinner_name'=>'M1'},
     '2'=>{'toplinner_name'=>'M1','f1_name'=>'M3','bottomlinner_name'=>'M2'},
     '3'=>{'toplinner_name'=>'M2','f1_name'=>'M3','bottomlinner_name'=>'M1'},
     '4'=>{'toplinner_name'=>'M2','f1_name'=>'M3','bottomlinner_name'=>'M2'},
  }

As you can see from the above out put 2 and 3 are same linners so i need t0 consider them as one how can i remove those duplicate values..... i tried below code

$input = array_map("unserialize", array_unique(array_map("serialize", $new)));

but it gives me same out put.

my desired out put should be

Array{
     '0'=>{'toplinner_name'=>'M1','f1_name'=>'M3','bottomlinner_name'=>'M1'},
     '1'=>{'toplinner_name'=>'M1','f1_name'=>'M3','bottomlinner_name'=>'M2'},
     '2'=>{'toplinner_name'=>'M2','f1_name'=>'M3','bottomlinner_name'=>'M2'},
  }
share|improve this question
    
Have you tried with only: $input = array_unique($new); ? – Gavriel Feb 1 '16 at 7:01
    
with that iam getting error "Notice: Array to string conversion " – sasi kanth Feb 1 '16 at 7:04
    
You should use the flag SORT_REGULAR like this: $input = array_unique($new, SORT_REGULAR); – Rein Feb 1 '16 at 7:13
    
it working but my desired out put is coming.it gives me same out put – sasi kanth Feb 1 '16 at 7:19
    
this will return u two records as [{"toplinner_name":"M1","f1_name":"M3","bottomlinner_name":"‌​M1"},{"toplinner_nam‌​e":"M1","f1_name":"M‌​3","bottomlinner_nam‌​e":"M2"}] – devpro Feb 1 '16 at 7:28

Example with your mentioned array in comments:

<?

// your array
$res[]=array('toplinner_name'=>'M1','f1_name'=>'M3','bottomlinner_name'=>'M1');
$res[]=array('toplinner_name'=>'M1','f1_name'=>'M3','bottomlinner_name'=>'M2');
$res[]=array('toplinner_name'=>'M1','f1_name'=>'M3','bottomlinner_name'=>'M1');
$res[]=array('toplinner_name'=>'M1','f1_name'=>'M3','bottomlinner_name'=>'M2');

// remove duplicates by using array_map and array_unique for multidimensional array
$duplicateRemoved = array_map("unserialize", array_unique(array_map("serialize", $res)));

// rearrange the array
$rearrangeArray = array_values($duplicateRemoved);

// encode the unique array into json format    
$encodedData = json_encode($rearrangeArray);

// print result    
echo $encodedData;    

?>
share|improve this answer
    
the above code will work if keys are same but my keys are diffrent – sasi kanth Feb 1 '16 at 7:39
    
@sasikanth: this will not effect on keys.. – devpro Feb 1 '16 at 7:47
    
i just added my desire out put in my question check once – sasi kanth Feb 1 '16 at 7:49
    
@sasikanth: i have tested with 0,2,3,4 indexes its works same – devpro Feb 1 '16 at 7:50
    
183.82.112.177/nick_samples/newalgo check this i tried your code here – sasi kanth Feb 1 '16 at 7:52

Those lines are different, see in bold:

'0'=>{'t'=>'M1','f'=>'M3','b'=>'M1'},

'2'=>{'t'=>'M1','f'=>'M3','b'=>'M2'},

'3'=>{'t'=>'M2','f'=>'M3','b'=>'M1'},

'4'=>{'t'=>'M2','f'=>'M3','b'=>'M2'},

share|improve this answer
    
i need to show either 2 or 1 not both . i just added my desire out in question check once – sasi kanth Feb 1 '16 at 7:42
    
I don't get it. What about '4'=>{'t'=>'M2','f'=>'M3','b'=>'M2'}, ? it's different from all the other lines in either t, or b or both. Can you explain again what exactly do you want? Not just by the expected output but by explaining why – Gavriel Feb 1 '16 at 7:53
    

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.