我需要一个看起来像......的数组
array( 11 => "fistVal", 19 => "secondVal", 120=> "thirdVal", 200 =>"fourthVal");
Run Code Online (Sandbox Code Playgroud)
并将其转换为......
array( 0 => "fistVal", 1 => "secondVal", 2=> "thirdVal", 3 =>"fourthVal");
Run Code Online (Sandbox Code Playgroud)
这就是我提出的 -
function compressArray($array){
if(count($array){
$counter = 0;
$compressedArray = array();
foreach($array as $cur){
$compressedArray[$count] = $cur;
$count++;
}
return $compressedArray;
} else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我只是好奇是否有任何内置功能在PHP或巧妙的技巧来做到这一点.
Ant*_*ney 11
您可以使用 array_values
直接从链接中获取的示例,
<?php
$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));
?>
Run Code Online (Sandbox Code Playgroud)
输出:
Array
(
[0] => XL
[1] => gold
)
Run Code Online (Sandbox Code Playgroud)