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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
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

PHP array is a very common data structure that is often used during the development process. However, as the amount of data increases, array performance can become an issue. This article will explore some performance optimization techniques for PHP arrays and provide specific code examples. 1. Use appropriate data structures In PHP, in addition to ordinary arrays, there are some other data structures, such as SplFixedArray, SplDoublyLinkedList, etc., which may perform better than ordinary arrays in certain situations.

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

As a leader in cryptocurrency trading, Gate.io offers a wide range of trading pairs, derivatives and financial services. The Chinese version of the website Sesame Open Door Gate is convenient for Chinese users and provides the same functions as Gate.io, but it is more suitable for Chinese people's habits. Users can access the Gate.io exchange or Sesame Open Gate official website through the designated website. Please be sure to keep your account information carefully and only visit the official website to ensure safety.

What is the maximum length of a PHP array What is the maximum length of a PHP array Aug 10, 2023 pm 02:53 PM

There is no fixed maximum length limit for arrays in PHP. The maximum length of the array is actually limited by the available memory, which is determined by the available memory of the server. If the array needs to store a very large number of elements, it may exceed the limit of the available memory of the server and Causes a runtime error.

How does the array_merge() function of PHP array merging work? How does the array_merge() function of PHP array merging work? Apr 28, 2024 pm 05:03 PM

PHP's array_merge() function merges two or more arrays into a new array. Create a new array. Iterate over the arrays to be merged. Add each element to a new array, overwriting existing elements if the keys are the same. Returns a new array containing all merged elements.

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

No, shuffling a PHP array order will not affect element references or addresses, since the elements and their keys remain unchanged. After shuffling, the contents of the array (elements and keys) remain unchanged, only the order of the keys changes.

What are the assignment methods for php arrays? What are the assignment methods for php arrays? Aug 16, 2023 pm 06:00 PM

PHP array assignment methods include direct assignment, array() function assignment, index assignment, range() function assignment, key-value pair assignment, loop assignment, etc. Detailed introduction: 1. Direct assignment, assign value directly when the array is declared, such as "$arr = [1, 2, 3];"; 2. Use the array() function to assign value, such as "$arr = array(1, 2, 3);"; 3. Use index assignment, etc.

How to convert php array to object in loop How to convert php array to object in loop Aug 10, 2023 pm 02:44 PM

There are two ways to convert a PHP array into an object in a loop: 1. Use forced type conversion to convert the array into an object, and the keys of the array must be valid object attribute names; 2. Create a new object and add the elements of the array Copied into the object, independent of whether the array keys are valid as property names of the object.

Common errors in PHP array reversal and their solutions Common errors in PHP array reversal and their solutions Apr 28, 2024 pm 09:36 PM

Three common mistakes when reversing PHP arrays: 1. Not using ArrayNotation, solution: use array notation to explicitly assign the reversed array to a new variable; 2. Try to reverse an associative array, solution: use array_flip() to convert first Reverse a simple array; 3. Reverse an empty array, solution: check whether the array is empty before reversing.

See all articles