Blogger Information
Blog 142
fans 5
comment 0
visits 130212
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP重置数组为连续数字索引的几种方式总结
php开发大牛
Original
883 people have browsed it

比如这样的一个php数组:

$arr = array(
 1 => 'apple',
 3 => 'banana',
 5 => 'orange'
);

想要转换为这样的数组:

$arr = array(
 0 => 'apple',
 1 => 'banana',
 2 => 'orange'
);

1、推荐的方式 array_values 方法

这样方式无论对普通数组还是关联数组都适用

<?php

$arr = array(
 1 => 'apple',
 3 => 'banana',
 5 => 'orange'
);

print_r(array_values($arr));

$arr1 = array(
 'name' => 'jerry',
 'age' => 16,
 'height' => '18cm'
);

print_r(array_values($arr1));


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post