数组换位有关问题
数组换位问题
- PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$array = array( 'ab'=>123, 'ef'=>234, 'hc'=>654)
我现在想让hc和ab换个位置,其中包括(key和value)
换位后的结果
- PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$array = array( 'hc'=>654, 'ef'=>234, 'ab'=>123,)
有哪些方法能办到?
方法之一:声明一个新数组,循环$array数组,做判断来换位!
本人喜欢找更高效的方法,特来发帖求教
------解决方案--------------------
php数组排序..................
你这个例子的可以使用"krsort()".
其余的情况就不一定了.
------解决方案--------------------
$array = array(
'ab'=>123,
'ef'=>234,
'hc'=>654
);
echo "
";<br>print_r($array);<br><br>ksort($array,1);<br>print_r($array);<br><font color="#e78608">------解决方案--------------------</font><br>
------解决方案--------------------
uksort()
自己订好规则.
------解决方案--------------------
关键是LZ你要根据什么规则来换位啊??
$array = array(
'ab'=>123,
'ef'=>234,
'hc'=>654,
'ht'=>456,
'ty'=>421,
);
那我是不是要ab hc 换位 // ef ht换位// hc ht 换位 ???
你总的有个参考吧
------解决方案--------------------
数组循环一次,O(n)复杂度,效率有什么问题?
------解决方案--------------------
我感觉foreach 就ok了 一个数组不存在什么效率问题吧

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.

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

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

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.
