需求
将
<code>$tempArray = array( 0=>"1:A", 1=>"2:B", 2=>"3:C", 3=>"4:D", 4=>"5:E", ); </code>
转成:
<code>$tempArray = array( "A"=>1, "B"=>2, "C"=>3, "D"=>4, "E"=>5, ); </code>
本来想使用php的内置函数array_map
来处理的,但是没有成功,除了遍历之外,我更想直接使用php的内置函数来实现,请问代码应该怎么写呢?
回复内容:
需求
将
<code>$tempArray = array( 0=>"1:A", 1=>"2:B", 2=>"3:C", 3=>"4:D", 4=>"5:E", ); </code>
转成:
<code>$tempArray = array( "A"=>1, "B"=>2, "C"=>3, "D"=>4, "E"=>5, ); </code>
本来想使用php的内置函数array_map
来处理的,但是没有成功,除了遍历之外,我更想直接使用php的内置函数来实现,请问代码应该怎么写呢?
一定要用array_map
(不支持key),这是一个方法.
<code>$tempArray = array( 0=>"1:A", 1=>"2:B", 2=>"3:C", 3=>"4:D", 4=>"5:E", ); $result = array(); array_map(function($value) use (&$result) { list($k,$v) = explode(":", $value); $result[$k] = $v; },$tempArray); </code>
<code>$array = array( 0=>"1:A", 1=>"2:B", 2=>"3:C", 3=>"4:D", 4=>"5:E", ); //处理 $array1 = array(); array_walk($array, function($val,$key,$fix) use(&$array1){ $v = explode($fix, $val); $array1[$v[1]] = $v[0]; },':'); var_dump($array1); /* 输出 array (size=5) 'A' => string '1' (length=1) 'B' => string '2' (length=1) 'C' => string '3' (length=1) 'D' => string '4' (length=1) 'E' => string '5' (length=1) */ </code>
补充:
<code>array_walk($array, function($val,$key,$arr){ $v = explode(":", $val); $arr[0][$v[1]] = $v[0]; },array(&$array1)); </code>
这么处理也是可以的
<code>$arr = ["1:A", "2:B", "3:C", "4:D", "5:E"]; $arr = json_decode("{".implode(',', array_map(function($item){ return preg_replace('/(.+?):(.+?)/', '"$2":"$1"', $item); }, $arr))."}", true); print_r($arr); /** output Array ( [A] => 1 [B] => 2 [C] => 3 [D] => 4 [E] => 5 ) **/ </code>
如果值里头的:
唯一的话可以这么做,为了显得狂拽一点缩成一句话了,可能有点难看懂。
用array_map
和explode
拆冒号
用array_combine
拼接

The article discusses PHP Data Objects (PDO), an extension for database access in PHP. It highlights PDO's role in enhancing security through prepared statements and its benefits over MySQLi, including database abstraction and better error handling.

Memcache and Memcached are PHP caching systems that speed up web apps by reducing database load. A single instance can be shared among projects with careful key management.

Article discusses steps to create and manage MySQL databases using PHP, focusing on connection, creation, common errors, and security measures.

The article discusses how JavaScript and PHP interact indirectly through HTTP requests due to their different environments. It covers methods for sending data from JavaScript to PHP and highlights security considerations like data validation and prot

The article discusses executing PHP scripts from the command line, including steps, common options, troubleshooting errors, and security considerations.

PEAR is a PHP framework for reusable components, enhancing development with package management, coding standards, and community support.

PHP is a versatile scripting language used mainly for web development, creating dynamic pages, and can also be utilized for command-line scripting, desktop apps, and API development.

The article discusses PHP's evolution from "Personal Home Page Tools" in 1995 to "PHP: Hypertext Preprocessor" in 1998, reflecting its expanded use beyond personal websites.


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

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

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
