Easiest way to implode() a two-dimensional array?

后端 未结 7 1596
眼角桃花
眼角桃花 2020-12-16 11:26

I\'m new to PHP, and don\'t have quite the grip on how it works. If I have a two dimensional array as such (returned by a database):

array(3) {   
    [0]=&         


        
相关标签:
7条回答
  • 2020-12-16 12:23

    Check out the below from the PHP implode() manual:

    <?php
    /**
     * Implode an array with the key and value pair giving
     * a glue, a separator between pairs and the array
     * to implode.
     * @param string $glue The glue between key and value
     * @param string $separator Separator between pairs
     * @param array $array The array to implode
     * @return string The imploded array
     */
    function array_implode( $glue, $separator, $array ) {
        if ( ! is_array( $array ) ) return $array;
        $string = array();
        foreach ( $array as $key => $val ) {
            if ( is_array( $val ) )
                $val = implode( ',', $val );
            $string[] = "{$key}{$glue}{$val}";
    
        }
        return implode( $separator, $string );
    
    }
    ?>
    

    If you only want to return the value (and not the key), just modify the above to use $string[] = "{$val}";.

    0 讨论(0)
提交回复
热议问题