内爆()二维数组的最简单方法?

时间:2021-12-10 21:36:14

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):

我是PHP的新手,并没有完全掌握它的工作原理。如果我有一个二维数组(由数据库返回):

array(3) {   
    [0]=> array(1) {         
        ["tag_id"]=> string(1) "5" 
    } 
    [1]=> array(1) {         
        ["tag_id"]=> string(1) "3" 
    } 
    [2]=> array(1) {         
        ["tag_id"]=> string(1) "4" 
    } 
}

and want to turn it into the string 5,3,4 what would be the quickest way do do this? I currently have an obnoxious foreach loop, but was hoping it could be done in one line. A standard implode gives me Array,Array,Array.

并希望把它变成字符串5,3,4这是最快的方法吗?我目前有一个令人讨厌的foreach循环,但希望它可以在一行中完成。标准的内爆给了我Array,Array,Array。

7 个解决方案

#1


26  

This modifies your array using array_map, but probably for the better by turning it into a 1D array of tag_id's. Then you can just use implode like normal:

这会使用array_map修改你的数组,但可能会把它变成tag_id的1D数组。然后你可以像正常一样使用implode:

$arr = array_map(function($el){ return $el['tag_id']; }, $arr);
$str = implode(',', $arr);

If you don't want to modify your array than you can just do this:

如果您不想修改数组,则可以执行以下操作:

$str = implode(',', array_map(function($el){ return $el['tag_id']; }, $arr));

Codepad Demo

键盘演示

#2


6  

You asked for a two-dimensional array, here's a function that will work for multidimensional array.

你问了一个二维数组,这是一个适用于多维数组的函数。

function implode_r($g, $p) {
    return is_array($p) ?
           implode($g, array_map(__FUNCTION__, array_fill(0, count($p), $g), $p)) : 
           $p;
}

I can flatten an array structure like so:

我可以像这样展平数组结构:

$multidimensional_array = array(
    'This',
    array(
        'is',
        array(
            'a',
            'test'
        ),
        array(
            'for',
            'multidimensional',
            array(
                'array'
            )
        )
    )
);

echo implode_r(',', $multidimensional_array);

The results is:

结果是:

This,is,a,test,for,multidimensional,array

#3


5  

// simplest
$str = implode(',',array_map('implode',$arr));

#4


2  

One line command

一行命令

implode(',', array_map('implode', $arr, array_fill(0, count($arr), '')))

#5


2  

If one simply wants to implode a single array "column" as in this case, then the simplest thing to do is:

如果只想在这种情况下内爆单个数组“列”,那么最简单的事情是:

implode(",", array_column($array,"tag_id")); 

#6


1  

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

从PHP implode()手册中查看以下内容:

<?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}";.

如果您只想返回值(而不是键),只需修改上面的内容即可使用$ string [] =“{$ val}”;.

#7


0  

function recursive_implode($connector=',', $array=[], $implod='keys'){

    if($implod=='keys'){

        $results=implode($connector,array_keys($array));
    }
    else{

        $results=implode($connector,$array);
    }

    foreach($array as $key=> $value){

        if(is_array($value)){

            $results.=$connector.recursive_implode($connector,$value,$implod);
        }
    }

    return $results;
}

#1


26  

This modifies your array using array_map, but probably for the better by turning it into a 1D array of tag_id's. Then you can just use implode like normal:

这会使用array_map修改你的数组,但可能会把它变成tag_id的1D数组。然后你可以像正常一样使用implode:

$arr = array_map(function($el){ return $el['tag_id']; }, $arr);
$str = implode(',', $arr);

If you don't want to modify your array than you can just do this:

如果您不想修改数组,则可以执行以下操作:

$str = implode(',', array_map(function($el){ return $el['tag_id']; }, $arr));

Codepad Demo

键盘演示

#2


6  

You asked for a two-dimensional array, here's a function that will work for multidimensional array.

你问了一个二维数组,这是一个适用于多维数组的函数。

function implode_r($g, $p) {
    return is_array($p) ?
           implode($g, array_map(__FUNCTION__, array_fill(0, count($p), $g), $p)) : 
           $p;
}

I can flatten an array structure like so:

我可以像这样展平数组结构:

$multidimensional_array = array(
    'This',
    array(
        'is',
        array(
            'a',
            'test'
        ),
        array(
            'for',
            'multidimensional',
            array(
                'array'
            )
        )
    )
);

echo implode_r(',', $multidimensional_array);

The results is:

结果是:

This,is,a,test,for,multidimensional,array

#3


5  

// simplest
$str = implode(',',array_map('implode',$arr));

#4


2  

One line command

一行命令

implode(',', array_map('implode', $arr, array_fill(0, count($arr), '')))

#5


2  

If one simply wants to implode a single array "column" as in this case, then the simplest thing to do is:

如果只想在这种情况下内爆单个数组“列”,那么最简单的事情是:

implode(",", array_column($array,"tag_id")); 

#6


1  

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

从PHP implode()手册中查看以下内容:

<?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}";.

如果您只想返回值(而不是键),只需修改上面的内容即可使用$ string [] =“{$ val}”;.

#7


0  

function recursive_implode($connector=',', $array=[], $implod='keys'){

    if($implod=='keys'){

        $results=implode($connector,array_keys($array));
    }
    else{

        $results=implode($connector,$array);
    }

    foreach($array as $key=> $value){

        if(is_array($value)){

            $results.=$connector.recursive_implode($connector,$value,$implod);
        }
    }

    return $results;
}