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

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
vijayalakshmi
Forum Newbie
Posts: 8
Joined: Tue Jan 21, 2014 10:22 pm

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

Post 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....
Last edited by requinix on Tue Feb 18, 2014 1:12 pm, edited 1 time in total.
Reason: please use [syntax] tags when posting code
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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?
vijayalakshmi
Forum Newbie
Posts: 8
Joined: Tue Jan 21, 2014 10:22 pm

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

Post by vijayalakshmi »

I got the solution...
Post Reply