Flatten a multi-dimensional array into a single level

@param  array  $array
@param  int  $depth
@return array
432219
public static function flatten(array $array, $depth = INF) : array
    {
        $depth = is_int($depth) ? max($depth, 0) : INF;

        return array_reduce($array, function ($result, $item) use ($depth) {
            if (!is_array($item) || $depth === 0) {
                return array_merge($result, [$item]);
            } elseif ($depth === 1) {
                return array_merge($result, array_values($item));
            } else {
                return array_merge($result, self::flatten($item, $depth - 1));
            }
        }, []);
    }
1100568
public static function flatten($array)
    {
        $return = [];

        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $return = \array_merge($return, static::flatten($value));
            } elseif ($value instanceof Collection) {
                $return = \array_merge($return, static::flatten($value->all()));
            } else {
                $return[$key] = $value;
            }
        }

        return $return;
    }
320901
public static function flatten(array $arr)
    {
        $flattened = [];

        array_walk_recursive($arr, function ($a) use (&$flattened) {
            $flattened[] = $a;
        });

        return $flattened;
    }
316242
static function traversableToArray($traversable, $depth = 0) {
		$vrat = array();
		if ($depth > 10) throw new \RuntimeException("Recursion is too deep.");
		if (!is_array($traversable) and !($traversable instanceof \Traversable)) {
			throw new \InvalidArgumentException("\$traversable must be an array or Traversable object.");
		}
		foreach ($traversable as $i=>$r) {
			if (is_array($r) or ($r instanceof \Traversable)) {
				$vrat[$i] = self::traversableToArray($r, $depth + 1);
			} else {
				$vrat[$i] = $r;
			}
		}
		return $vrat;
	}
42410
protected function normalize($data, int $depth = 0)
    {
        if ($depth > $this->maxNormalizeDepth) {
            return 'Over '.$this->maxNormalizeDepth.' levels deep, aborting normalization';
        }

        if (is_array($data) || $data instanceof \Traversable) {
            $normalized = [];

            $count = 1;
            foreach ($data as $key => $value) {
                if ($count++ > $this->maxNormalizeItemCount) {
                    $normalized['...'] = 'Over '.$this->maxNormalizeItemCount.' items ('.count($data).' total), aborting normalization';
                    break;
                }

                $normalized[$key] = $this->normalize($value, $depth + 1);
            }

            return $normalized;
        }

        if ($data instanceof Throwable) {
            return $this->normalizeException($data, $depth);
        }

        return $data;
    }
930776
public static function flatten($array, $return = array())
    {
        foreach ($array as $value) {
            if (is_array($value) || is_object($value)) {
                $return = self::flatten($value, $return);
            } else {
                $return[] = $value;
            }
        }
        return $return;
    }
500060
public static function merge(array $array1, array $array2, $deep = false)
    {
        // Normal array merge
        if (!$deep) {
            return array_merge($array1, $array2);
        }

        // Perform a deep merge
        $merged = $array1;

        foreach ($array2 as $key => $value) {
            if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
                $merged[$key] = static::merge($merged[$key], $value, $deep);
            } else if (is_numeric($key)) {
                if (!in_array($value, $merged)) {
                    $merged[] = $value;
                }
            } else {
                $merged[$key] = $value;
            }
        }

        return $merged;
    }
1326310
public static function flatten(array $array, string $separator = null, string $prepend = ''): array
    {
        $flattened = [];

        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $flattened = array_merge($flattened, static::flatten($value, $separator, $prepend . $key . $separator));
            } else {
                $flattened[$prepend . $key] = $value;
            }
        }

        return $flattened;
    }
1309701
private function normalizeObject($data, $depth) {
		if (is_object($data)) {
			if ($data instanceof \Exception) {
				return $this->normalizeException($data);
			}
			// We don't need to go too deep in the recursion
			$maxObjectRecursion = $this->maxRecursionDepth;
			$arrayObject = new \ArrayObject($data);
			$serializedObject = $arrayObject->getArrayCopy();
			if ($depth < $maxObjectRecursion) {
				$depth++;
				$response = $this->normalize($serializedObject, $depth);

				return [$this->getObjetName($data) => $response];
			}

			return $this->getObjetName($data);
		}

		return null;
	}
258106
public function array_depth($array)
    {
        $max_depth = 1;
        foreach ($array as $value) {
            if (is_array($value)) {
                $depth = $this->array_depth($value) + 1;
                if ($depth > $max_depth) {
                    $max_depth = $depth;
                }
            }
        }
        return $max_depth;
    }
337076
public static function array_flatten(array $array){
		$index = 0;
		$count = count($array);

		while ($index < $count) {
			if (is_array($array[$index])) {
				array_splice($array, $index, 1, $array[$index]);
			} else {
				++$index;
			}
			$count = count($array);
		}
		return $array;
	}
523009
public static function array_prepend( $array, $string, $deep = false )
    {
        if ( empty( $array ) || empty( $string ) )
        {
            return $array;
        }

        foreach ( $array as $key => $element )
        {
            if ( is_array( $element ) )
            {
                if ( $deep )
                {
                    $array[$key] = self::array_prepend( $element, $string, $deep );
                }
                else
                {
                    trigger_error( 'array_prepend: array element', E_USER_WARNING );
                }
            }
            else
            {
                $array[$key] = $string . $element;
            }
        }

        return $array;
    }
260471
public function flatter($array)
    {
        $result = [];
        foreach ($array as $item) {
            if (is_array($item)) {
                $result = array_merge($result, $this->flatter($item));
            } else {
                $result[] = $item;
            }
        }

        return $result;
    }
706344
private function writeArray(array $array, $depth, $comments = false)
    {
        // Else dump each value on its own line
        $isIndexed = array_values($array) === $array;
        $method = $comments ? 'writeComment' : 'writeLine';

        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $val = '';
            } else {
                $val = $value;
            }

            $key = $this->serializeValue($key);
            $val = $this->serializeValue($val);
            if ($isIndexed) {
                $this->$method($val.',', $depth);
            } else {
                $this->$method(sprintf('%s => %s,', $key, $val), $depth);
            }

            if (is_array($value)) {
                $this->writeArray($value, $depth + 1);
            }
        }
    }
331357
public function extractAll(array $array, int $depth = 1)
    {
        $depth--;
        $res = [];
        foreach ($array as $key => $object) {
            $res[$key] = $depth>0 ? $this->extractAll($object, $depth) : $this->extract($object);
        }

        return $res;
    }
1172716
public function flatten(array $elements)
    {
        $level = [];

        foreach ($elements as $node)
        {
            if (is_array($node))
            {
                $level = array_merge($level, $this->flatten($node));
            }
            else
            {
                $level[] = $node;
            }
        }

        return $level;
    }
679579
public function mergeRecursive($array)
    {
        if ($this->isInstanceOf($array, $this)) {
            $array = $array->val();
        }

        $this->val(array_merge_recursive($this->val(), $array));

        return $this;
    }