PHP使用array_merge重新排列数组下标的方法_php技巧
本文实例讲述了PHP使用array_merge重新排列数组下标的方法。分享给大家供大家参考。具体如下:
用了一个array_unique去除了一个数组里面的重复,但是发现下标保留了原数组的下标,但是php使用for循环需要下标整齐,所以寻找重新排列数组下标的方法array_merge可以解决这个问题
array_merge() 函数把两个或多个数组合并为一个数组。
如果键名有重复,该键的键值为最后一个键名对应的值(后面的覆盖前面的)。如果数组是数字索引的,则键名会以连续方式重新索引。
注释:如果仅仅向 array_merge() 函数输入了一个数组,且键名是整数,则该函数将返回带有整数键名的新数组,其键名以 0 开始进行重新索引。(参见例子 2)
语法:
array_merge(array1,array2,array3...)
参数 | 描述 |
---|---|
array1 | 必需。输入的第一个数组。 |
array2 | 必需。输入的第二个数组。 |
array3 | 可选。可指定的多个输入数组。 |
例子 1
<?php $a1=array("a"=>"Horse","b"=>"Dog"); $a2=array("c"=>"Cow","b"=>"Cat"); print_r(array_merge($a1,$a2)); ?>
输出:
Array ( [a] => Horse [b] => Cat [c] => Cow )
例子 2
仅使用一个数组参数:
<?php $a=array(3=>"Horse",4=>"Dog"); print_r(array_merge($a)); ?>
输出:
Array ( [0] => Horse [1] => Dog )
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
