当前位置: 动力学知识库 > 问答 > 编程问答 >

How to re-index an array in php?

问题描述:

This question already has an answer here:

  • How do you reindex an array in PHP?

    16 answers

网友答案:

Use array_values:

$arr = array_values($arr);
网友答案:

It's as simple as taking the current values as an array an replacing your current array with this:

$array = array_values($array);
网友答案:

TRY

<?php 
$arr=array(array("img","dummies","slides","01.jpg"),array("img","dummies","slides","01.jpg"),array("img","dummies","slides","01.jpg"));
print_r($arr);
echo $num_arr=count($arr);
for($i=0;$i<$num_arr;$i++)
{
    $new_key="key_".$i;
    $arr[$new_key] = $arr[$i];
    unset($arr[$i]);
}
print_r($arr);


?>
网友答案:

try this

$array = array( 0 => 'string1', 2 => 'string2', 4 => 'string3', 5 => 'string4');
$arrays =$array;
print_r($array);
$array=array();
$i=0;
    foreach($arrays as $k => $item)
    {
    $array[$i]=$item;
        unset($arrays[$k]);
        $i++;

    }

print_r($array);

Demo

分享给朋友:
您可能感兴趣的文章:
随机阅读: