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

WBOY
Release: 2016-06-23 14:00:18
Original
1119 people have browsed it

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);
Copy after login
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);
Copy after login


版主的更漂亮!

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

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

Related labels:
1 3
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!