Page 1 of 1

How to disperse the pair or not from array list using php

Posted: Mon Feb 17, 2014 10:08 pm
by vijayalakshmi
Hi,
I hav completed the dispersed pair or not from array list. But it is not given the correct array (same number of key) as when I enter the input array list. Here I am forwarding my code,,

Code: Select all

function matchUp($array)    {
    $result = array();
    while($el = array_pop($array)) {
        shuffle($array);
        if (sizeof($array) > 0) {
            $candidate = array_pop($array);
            $result[] = array(array_pop($el),array_pop($candidate));
            if (sizeof($el) > 0) {
                $array[] = $el;
            }
            if (sizeof($candidate) > 0) {
                $array[] = $candidate;
            }
        }
        else {
            $result[] = array(array_pop($el));
        }
    }
    return $result;
} 

$input_array = array("S1","S2","S3","S4","S5","S6","S7","S8");
$pair_set = array_slice($input_array, 0,6);
$dispersed_set = array_slice($input_array,6);
$array_p = array_chunk($pair_set, 2);
$array_s = array_chunk($dispersed_set, 1); 
echo '<pre>';
$array = array_merge($array_p,$array_s);
// It is an input array list....
print_r($array);
echo '</pre>';
// It is an output array list....
print_r(matchup($array));
But, the output comes as 0 to 3 key values in matchup($array),,, I need 0 to 4 array keys with pair set or not. Pls tell me the solution asap....

Re: How to disperse the pair or not from array list using ph

Posted: Tue Feb 18, 2014 1:21 pm
by requinix
Your description is just as cryptic as your code.

What is the output you want for that array and what is the sequence of steps to follow to get it?

Re: How to disperse the pair or not from array list using ph

Posted: Tue Feb 18, 2014 10:02 pm
by vijayalakshmi
I got the solution...