public static function group(array $array, $key = null, $forceArray = false)
    {
        $results = [];
        $hasArray = [];

        foreach ($array as $index => $value) {
            // List value
            if (is_array($value) || is_object($value)) {
                if (!static::has($value, $key)) {
                    continue;
                }

                $resultKey = static::get($value, $key);
                $resultValue = $array[$index];
            } else {
                // Scalar value.
                $resultKey = $value;
                $resultValue = $index;
            }

            // First set value if not exists.
            if (!isset($results[$resultKey])) {
                // Force first element in array.
                if ($forceArray) {
                    $results[$resultKey] = [$resultValue];
                    $hasArray[$resultKey] = true;
                } else {
                    // Keep first element single.
                    $results[$resultKey] = $resultValue;
                }
            } elseif (!$forceArray && empty($hasArray[$resultKey])) {
                // If first element is sngle, now add second element as an array.
                $results[$resultKey] = [
                    $results[$resultKey],
                    $resultValue,
                ];

                $hasArray[$resultKey] = true;
            } else {
                // Now always push new elements.
                $results[$resultKey][] = $resultValue;
            }
        }

        unset($hasArray);

        return $results;
    }
public static function group(array $array, $key = null, $forceArray = false)
    {
        $results = [];
        $hasArray = [];

        foreach ($array as $index => $value) {
            // List value
            if (is_array($value) || is_object($value)) {
                if (!static::has($value, $key)) {
                    continue;
                }

                $resultKey = static::get($value, $key);
                $resultValue = $array[$index];
            } else {
                // Scalar value.
                $resultKey = $value;
                $resultValue = $index;
            }

            // First set value if not exists.
            if (!isset($results[$resultKey])) {
                // Force first element in array.
                if ($forceArray) {
                    $results[$resultKey] = [$resultValue];
                    $hasArray[$resultKey] = true;
                } else {
                    // Keep first element single.
                    $results[$resultKey] = $resultValue;
                }
            } elseif (!$forceArray && empty($hasArray[$resultKey])) {
                // If first element is sngle, now add second element as an array.
                $results[$resultKey] = [
                    $results[$resultKey],
                    $resultValue,
                ];

                $hasArray[$resultKey] = true;
            } else {
                // Now always push new elements.
                $results[$resultKey][] = $resultValue;
            }
        }

        unset($hasArray);

        return $results;
    }
public static function group ( array $ array , $ key = null , $ forceArray = false ) { $ results = [ ] ; $ hasArray = [ ] ; foreach ( $ array as $ index => $ value ) { // List value if ( is_array ( $ value ) || is_object ( $ value ) ) { if ( ! static :: has ( $ value , $ key ) ) { continue ; } $ resultKey = static :: get ( $ value , $ key ) ; $ resultValue = $ array [ $ index ] ; } else { // Scalar value. $ resultKey = $ value ; $ resultValue = $ index ; } // First set value if not exists. if ( ! isset ( $ results [ $ resultKey ] ) ) { // Force first element in array. if ( $ forceArray ) { $ results [ $ resultKey ] = [ $ resultValue ] ; $ hasArray [ $ resultKey ] = true ; } else { // Keep first element single. $ results [ $ resultKey ] = $ resultValue ; } } elseif ( ! $ forceArray && empty ( $ hasArray [ $ resultKey ] ) ) { // If first element is sngle, now add second element as an array. $ results [ $ resultKey ] = [ $ results [ $ resultKey ] , $ resultValue , ] ; $ hasArray [ $ resultKey ] = true ; } else { // Now always push new elements. $ results [ $ resultKey ] [ ] = $ resultValue ; } } unset ( $ hasArray ) ; return $ results ; }
Re-group an array to create a reverse lookup of an array of scalars, arrays or objects.

@param array  $array      The source array data.
@param string $key        Where the elements of the source array are objects or arrays, the key to pivot on.
@param bool   $forceArray Force child element always array.

@return array An array of arrays grouped either on the value of the keys,
or an individual key of an object or array.

@since  2.0
Re - group an array to create a reverse lookup of an array of scalars arrays or objects .