src/Utilities/Arrays.php
public static function array2JavaScript(array $array, $jsVariableName = '', $preserveIndexes = false)
    {
        /*
         * No elements?
         * Nothing to do
         */
        if (empty($array)) {
            return null;
        }

        $result = '';
        $counter = 0;

        $arrayCount = count($array);
        $arrayPrepared = self::quoteStrings($array);
        $isMultiDimensional = self::isMultiDimensional($arrayPrepared);

        /*
         * Name of the variable was not provided and it's a multi dimensional array?
         * Let's create the name, because variable is required for later usage (related to multi dimensional array)
         */
        if (empty($jsVariableName) && $isMultiDimensional) {
            $jsVariableName = 'autoGeneratedVariable';
        }

        if (!empty($jsVariableName) && is_string($jsVariableName)) {
            $result .= sprintf('var %s = ', $jsVariableName);
        }

        $result .= 'new Array(';

        if ($preserveIndexes || $isMultiDimensional) {
            $result .= $arrayCount;
            $result .= ');';
        }

        foreach ($arrayPrepared as $index => $value) {
            ++$counter;

            if (is_array($value)) {
                $variable = $index;

                if (is_int($index)) {
                    $variable = 'value_' . $variable;
                }

                $value = self::array2JavaScript($value, $variable, $preserveIndexes);

                if (null !== $value && '' !== $value) {
                    /*
                     * Add an empty line for the 1st iteration only. Required to avoid missing empty line after
                     * declaration of variable:
                     *
                     * var autoGeneratedVariable = new Array(...);autoGeneratedVariable[0] = new Array(...);
                     * autoGeneratedVariable[1] = new Array(...);
                     */
                    if (1 === $counter) {
                        $result .= "\n";
                    }

                    $result .= $value . "\n";
                    $result .= sprintf('%s[%s] = %s;', $jsVariableName, Miscellaneous::quoteValue($index), $variable);

                    if ($counter !== $arrayCount) {
                        $result .= "\n";
                    }
                }
            } elseif ($preserveIndexes) {
                if (!empty($jsVariableName)) {
                    $index = Miscellaneous::quoteValue($index);
                    $result .= sprintf("\n%s[%s] = %s;", $jsVariableName, $index, $value);
                }
            } else {
                $format = '%s';

                if ($counter < $arrayCount) {
                    $format .= ', ';
                }

                $result .= sprintf($format, $value);
            }
        }

        if (!$preserveIndexes && !$isMultiDimensional) {
            $result .= ');';
        }

        return $result;
    }
public static function array2JavaScript(array $array, $jsVariableName = '', $preserveIndexes = false)
    {
        /*
         * No elements?
         * Nothing to do
         */
        if (empty($array)) {
            return null;
        }

        $result = '';
        $counter = 0;

        $arrayCount = count($array);
        $arrayPrepared = self::quoteStrings($array);
        $isMultiDimensional = self::isMultiDimensional($arrayPrepared);

        /*
         * Name of the variable was not provided and it's a multi dimensional array?
         * Let's create the name, because variable is required for later usage (related to multi dimensional array)
         */
        if (empty($jsVariableName) && $isMultiDimensional) {
            $jsVariableName = 'autoGeneratedVariable';
        }

        if (!empty($jsVariableName) && is_string($jsVariableName)) {
            $result .= sprintf('var %s = ', $jsVariableName);
        }

        $result .= 'new Array(';

        if ($preserveIndexes || $isMultiDimensional) {
            $result .= $arrayCount;
            $result .= ');';
        }

        foreach ($arrayPrepared as $index => $value) {
            ++$counter;

            if (is_array($value)) {
                $variable = $index;

                if (is_int($index)) {
                    $variable = 'value_' . $variable;
                }

                $value = self::array2JavaScript($value, $variable, $preserveIndexes);

                if (null !== $value && '' !== $value) {
                    /*
                     * Add an empty line for the 1st iteration only. Required to avoid missing empty line after
                     * declaration of variable:
                     *
                     * var autoGeneratedVariable = new Array(...);autoGeneratedVariable[0] = new Array(...);
                     * autoGeneratedVariable[1] = new Array(...);
                     */
                    if (1 === $counter) {
                        $result .= "\n";
                    }

                    $result .= $value . "\n";
                    $result .= sprintf('%s[%s] = %s;', $jsVariableName, Miscellaneous::quoteValue($index), $variable);

                    if ($counter !== $arrayCount) {
                        $result .= "\n";
                    }
                }
            } elseif ($preserveIndexes) {
                if (!empty($jsVariableName)) {
                    $index = Miscellaneous::quoteValue($index);
                    $result .= sprintf("\n%s[%s] = %s;", $jsVariableName, $index, $value);
                }
            } else {
                $format = '%s';

                if ($counter < $arrayCount) {
                    $format .= ', ';
                }

                $result .= sprintf($format, $value);
            }
        }

        if (!$preserveIndexes && !$isMultiDimensional) {
            $result .= ');';
        }

        return $result;
    }
public static function array2JavaScript ( array $ array , $ jsVariableName = '' , $ preserveIndexes = false ) { /*
         * No elements?
         * Nothing to do
         */ if ( empty ( $ array ) ) { return null ; } $ result = '' ; $ counter = 0 ; $ arrayCount = count ( $ array ) ; $ arrayPrepared = self :: quoteStrings ( $ array ) ; $ isMultiDimensional = self :: isMultiDimensional ( $ arrayPrepared ) ; /*
         * Name of the variable was not provided and it's a multi dimensional array?
         * Let's create the name, because variable is required for later usage (related to multi dimensional array)
         */ if ( empty ( $ jsVariableName ) && $ isMultiDimensional ) { $ jsVariableName = 'autoGeneratedVariable' ; } if ( ! empty ( $ jsVariableName ) && is_string ( $ jsVariableName ) ) { $ result .= sprintf ( 'var %s = ' , $ jsVariableName ) ; } $ result .= 'new Array(' ; if ( $ preserveIndexes || $ isMultiDimensional ) { $ result .= $ arrayCount ; $ result .= ');' ; } foreach ( $ arrayPrepared as $ index => $ value ) { ++ $ counter ; if ( is_array ( $ value ) ) { $ variable = $ index ; if ( is_int ( $ index ) ) { $ variable = 'value_' . $ variable ; } $ value = self :: array2JavaScript ( $ value , $ variable , $ preserveIndexes ) ; if ( null !== $ value && '' !== $ value ) { /*
                     * Add an empty line for the 1st iteration only. Required to avoid missing empty line after
                     * declaration of variable:
                     *
                     * var autoGeneratedVariable = new Array(...);autoGeneratedVariable[0] = new Array(...);
                     * autoGeneratedVariable[1] = new Array(...);
                     */ if ( 1 === $ counter ) { $ result .= "\n" ; } $ result .= $ value . "\n" ; $ result .= sprintf ( '%s[%s] = %s;' , $ jsVariableName , Miscellaneous :: quoteValue ( $ index ) , $ variable ) ; if ( $ counter !== $ arrayCount ) { $ result .= "\n" ; } } } elseif ( $ preserveIndexes ) { if ( ! empty ( $ jsVariableName ) ) { $ index = Miscellaneous :: quoteValue ( $ index ) ; $ result .= sprintf ( "\n%s[%s] = %s;" , $ jsVariableName , $ index , $ value ) ; } } else { $ format = '%s' ; if ( $ counter < $ arrayCount ) { $ format .= ', ' ; } $ result .= sprintf ( $ format , $ value ) ; } } if ( ! $ preserveIndexes && ! $ isMultiDimensional ) { $ result .= ');' ; } return $ result ; }
Generates JavaScript code for given PHP array

@param array  $array           The array that should be generated to JavaScript
@param string $jsVariableName  (optional) Name of the variable that will be in generated JavaScript code
@param bool   $preserveIndexes (optional) If is set to true and $jsVariableName isn't empty, indexes also
will be added to the JavaScript code. Otherwise not.
@return null|string
Generates JavaScript code for given PHP array