Home  >  Article  >  Backend Development  >  PHP数组,$a = {0,1,2,3}怎样变成$a = {1,2,3,4]

PHP数组,$a = {0,1,2,3}怎样变成$a = {1,2,3,4]

WBOY
WBOYOriginal
2016-06-23 14:00:181121browse

PHP数组,$a = {0,1,2,3}怎样变成$a = {1,2,3,4]。就是多维数组的维,然后把维加1,输出成表格的序列号,1,2,3,4


回复讨论(解决方案)

$a = array(0, 1, 2, 3);$b = range(1, count($a));$c = array_combine($b, $a);print_r($c);
Array
(
    [1] => 0
    [2] => 1
    [3] => 2
    [4] => 3
)

$arr = array(0,1,2,3);$temp = array();foreach($arr as $k=>$v){    $k++;    $temp[] = $k;}print_r($temp);


版主的更漂亮!

版主的思路漂亮,这里提供另一种思路。

<?php$a = array(0,1,2,3);array_unshift($a, 0); // 在数组开头插入一个新元素,使key自增1unset($a[0]);         // 删除开头新增的元素,但key保持不变print_r($a);          // 输出?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn