将数组从一种格式转换为另一种格式

时间:2014-12-22 07:32:43

标签: php arrays arraylist

在PHP中,我有一个像这样的数组

Array
(
    [0] => Array
        (
            [name] => Printed Chiffon Dress
        )

    [1] => Array
        (
            [name] => Printed Dress
        )

    [2] => Array
        (
            [name] => Printed Dress
        )

    [3] => Array
        (
            [name] => Printed Summer Dress
        )

    [4] => Array
        (
            [name] => Printed Summer Dress
        )

)

我想将该数组转换为这种格式

Array
(
    [0] => Printed Chiffon Dress
    [1] => Printed Dress
    [2] => Printed Dress
    [3] => Printed Summer Dress
    [4] => Printed Summer Dress
)

那么有人可以告诉我如何将数组转换为上述格式吗?任何帮助和建议都会非常明显。感谢

1 个答案:

答案 0 :(得分:1)

如果您使用的是PHP 5.5及更高版本,则可以使用array_column

$new_array = array_column($array, 'name');

如果您根本不需要任何功能,foreach会做得很好,然后将其推入另一个容器中:

$new_array = array();
foreach($array as $value) {
    $new_array[] = $value['name'];
}