Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial PHP数组,$a = {0,1,2,3}怎样变成$a = {1,2,3,4]

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

Jun 23, 2016 pm 02:00 PM
1 2 3

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

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are php array key-value pairs? What are php array key-value pairs? Aug 03, 2023 pm 02:20 PM

What are php array key-value pairs?

How to determine how many arrays there are in php How to determine how many arrays there are in php Aug 04, 2023 pm 05:40 PM

How to determine how many arrays there are in php

gateio exchange official website entrance Sesame door gate official website entrance gateio exchange official website entrance Sesame door gate official website entrance Feb 21, 2025 pm 02:48 PM

gateio exchange official website entrance Sesame door gate official website entrance

Effective implementation of array union in PHP Effective implementation of array union in PHP Apr 30, 2024 pm 01:03 PM

Effective implementation of array union in PHP

An exploration of performance optimization techniques for PHP arrays An exploration of performance optimization techniques for PHP arrays Mar 13, 2024 pm 03:03 PM

An exploration of performance optimization techniques for PHP arrays

gate official website login portal gate.io login web version gate official website login portal gate.io login web version Feb 18, 2025 pm 03:00 PM

gate official website login portal gate.io login web version

What is the function in PHP to determine if an array is empty? What is the function in PHP to determine if an array is empty? Aug 03, 2023 pm 05:15 PM

What is the function in PHP to determine if an array is empty?

Will shuffling the order of PHP array affect the reference or address of the array? Will shuffling the order of PHP array affect the reference or address of the array? Apr 30, 2024 pm 03:48 PM

Will shuffling the order of PHP array affect the reference or address of the array?

See all articles