-3

I have 2 flat arrays which are Leaders and Members. These arrays are populated while incrementing ids.

I would like to associate one leader id with a sequence of consecutive ids in the members array. If there are too many leaders and not enough consecutive sequences of members, relate the remaining leader to null.

Example:

$leaders = [1,4,8,13];
$members = [2,3,5,6,7,9,10,11,12];

This is the result I want

$leaders = [
   1 => [2,3],
   4 => [5,6,7],
   8 => [9,10,11,12],
   13 => null
];
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 5
    _"I want"_ is not a question. What have you tried? Where a you stuck? What is the problem? – KIKO Software Nov 01 '22 at 08:51
  • You don't seem to understand my previous comment. It's not your English I have a problem with. It would be nice if you show what you have tried to solve this. It's one of the steps to follow when you ask a question: _"Describe what you tried and what you expected to happen."_ – KIKO Software Nov 01 '22 at 09:03
  • 1
    So except for you telling us what the relation between leaders and members is, how should your code know? I think any answer at this point will be a shot in the dark. Maybe you can shed some light on this? – Techno Nov 01 '22 at 09:06
  • Related pages that require arrays to be split on consecutive numbers: [How to split an array based on the condition `+1`?](https://stackoverflow.com/q/65014026/2943403) and [Split an array in sub arrays of consecutive dates](https://stackoverflow.com/q/17729552/2943403) and [Split php array to group when value is range or no](https://stackoverflow.com/q/43741592/2943403) – mickmackusa Nov 01 '22 at 12:26

2 Answers2

0

Is this a solution for your question? I have assumed that you need to assign members to leaders up to the next leader number, i.e leader 1 is assigned to member 2,3. Next leader 4, is assigned members that have higher numbers than 4... i.e. 5,6,7 (but not higher than 8 - next leader), and so on...

$leaders = [1,4,8,13];
$members = [2,3,5,6,7,9,10,11,12];

$current_leader = current($leaders);
$next_leader = next($leaders);
$group = [];
foreach ($members as $member) {
    if ($member < $next_leader) {
        $group[$current_leader][] = $member;
    } else {
        $group[$next_leader][] = $member;
        $current_leader = $next_leader;
        $next_leader = next($leaders);
    }
}
if ($next_leader) {
    $group[$next_leader] = null;
}

// Output the result
echo "<pre>";
print_r($group);

Note that this solution won't solve if you have more leaders above 13... But it may be a help on the way :)

Gowire
  • 1,046
  • 6
  • 27
0

Loop the members array and conditional declare a reference variable that will point to the key in the result array which relates to the first leasers value. Using array_shift() will isolate the first leader and consume it (which will present a new first leader the next time a new leader value is needed.

When finished iterating the members array, try to loop the leaders array. If there are any remaining leaders, they will be pushed into the result array as keys to null values.

Code: (Demo)

$result = [];
$lastMember = null;
foreach ($members as $member) {
    if ($lastMember === null || $lastMember !== $member - 1) {
        unset($ref);
        $result[array_shift($leaders)] = &$ref;
    }
    $ref[] = $lastMember = $member;
}
foreach ($leaders as $leader) {
    $result[$member] = null;
}
var_export($result);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136