php - 压缩一个php数组

标签 php arrays

我需要一个看起来像...的数组

array( 11 => "fistVal", 19 => "secondVal", 120=> "thirdVal", 200 =>"fourthVal");

并将其转换为...

array( 0 => "fistVal", 1 => "secondVal", 2=> "thirdVal", 3 =>"fourthVal");

这是我想出的-

function compressArray($array){
    if(count($array){
        $counter = 0;
        $compressedArray = array();
        foreach($array as $cur){
            $compressedArray[$count] = $cur;
            $count++;   
        }
        return $compressedArray;
    } else {
        return false;
    }
}

我只是好奇 php 中是否有任何内置功能或巧妙的技巧可以做到这一点。

最佳答案

您可以使用 array_values

示例直接取自链接,

<?php
$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));
?>

输出:

Array
(
    [0] => XL
    [1] => gold
)

关于php - 压缩一个php数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2638747/

相关文章:

php - FCM Android通知声音问题

php - 如何为 json 格式化 Mysql/php 数组?

c - 生成随机数组并存储在c中

c - 从汇编代码中查找数组的维度

php - 在数据库列中搜索某些数字

php - 在 PHP 中舍入和格式化货币

php - 如何循环遍历 3 维数组以将数据插入数据库?

arrays - 为 r 中的索引对对数组进行子集

c++ - 如何在 C++ 中访问数组结构内部的数组结构?

php - 如何在 Silverstripe 中搜索表数据?