Verification: a143cc29221c9be0

Php array in groups of

Содержание

C++

#include

using namespace std;

void reverse(int arr[], int n, int k)

{

    for (int i = 0; i

    {

        int left = i;

        int right = min(i + k - 1, n - 1);

        while (left

            swap(arr[left++], arr[right--]);

    }

}

int main()

{

    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};

    int k = 3;

    int n = sizeof(arr) / sizeof(arr[0]);

    reverse(arr, n, k);

    for (int i = 0; i

        cout " ";

    return 0;

}

Java

class GFG {

    static void reverse(int arr[], int n, int k)

    {

        for (int i = 0; i

        {

            int left = i;

            int right = Math.min(i + k - 1, n - 1);

            int temp;

            while (left

            {

                temp=arr[left];

                arr[left]=arr[right];

                arr[right]=temp;

                left+=1;

                right-=1;

            }

        }

    }

    public static void main(String[] args)

    {

        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};

        int k = 3;

        int n = arr.length;

        reverse(arr, n, k);

        for (int i = 0; i

            System.out.print(arr[i] + " ");

    }

}

Python3

def reverse(arr, n, k):

    i = 0

    while(i

        left = i

        right = min(i + k - 1, n - 1)

        while (left

            arr[left], arr[right] = arr[right], arr[left]

            left+= 1;

            right-=1

        i+= k

arr = [1, 2, 3, 4, 5, 6,

                   7, 8]

k = 3

n = len(arr)

reverse(arr, n, k)

for i in range(0, n):

        print(arr[i], end =" ")

C#

using System;

class GFG

{

public static void reverse(int[] arr,

                           int n, int k)

{

    for (int i = 0; i

    {

        int left = i;

        int right = Math.Min(i + k - 1, n - 1);

        int temp;

        while (left

        {

            temp = arr[left];

            arr[left] = arr[right];

            arr[right] = temp;

            left += 1;

            right -= 1;

        }

    }

}

public static void Main(string[] args)

{

    int[] arr = new int[] {1, 2, 3, 4,

                           5, 6, 7, 8};

    int k = 3;

    int n = arr.Length;

    reverse(arr, n, k);

    for (int i = 0; i

    {

        Console.Write(arr[i] + " ");

    }

}

}

PHP

function reverse($arr, $n, $k)

{

    for ($i = 0; $i $n; $i += $k)

    {

        $left = $i;

        $right = min($i + $k - 1, $n - 1);

        $temp;

        while ($left $right)

        {

            $temp = $arr[$left];

            $arr[$left] = $arr[$right];

            $arr[$right] = $temp;

            $left += 1;

            $right -= 1;

        }

    }

    return $arr;

}

$arr = array(1, 2, 3, 4, 5, 6, 7, 8);

$k = 3;

$n = sizeof($arr);

$arr1 = reverse($arr, $n, $k);

for ($i = 0; $i $n; $i++)

    echo $arr1[$i] . " ";

?>

Breakdown

function groupArray($arr, $group, $preserveGroupKey = false, $preserveSubArrays = false)

This function accepts 2 to 4 parameters.

  1. The flat array you want to group (array)
  2. The key you want to group by (string/int)
  3. Option to preserve the group key in the output of each sub array (Boolean)
  4. Option to preserve sub arrays. If only 1 key exists in each sub array, the function will store just the single value for each row instead of an array (Boolean)

The first parameter is the array itself, the second parameter is the key that you want to group by, and the 3rd (optional) parameter is a boolean that tells the function if you want to preserve the group key in the sub arrays.