在这介绍一个PHP语言里比较常用的数组操作函数,array_map函数,当我第一次接触到这个函数时,对这个函数的强大功能所震慑,也越发感觉PHP语言的好玩之处。
说明
了解PHP函数的最好的去处就是PHP手册,PHP手册上的关于此函数的说明:
array array_map ( callable $callback , array $arr1 [, array $... ] )
array_map() 返回一个数组,该数组包含了 arr1 中的所有单元经过 callback 作用过之后的单元。callback 接受的参数数目应该和传递给 array_map() 函数的数组数目一致。就是说将$arr1数组的每个单元的值,通过回调函数作用后存储进一个新的数组作为新数组的对应单元的值而还有个厉害的地方是这个函数可以传入多个数组,也就是在你定义的回调函数中可以同时对多个数组的每个单元的值同时操作
操作范例
例子1:<?php function cube($n){ return $n*$n;}$num = array(1,2,3,4,5);var_dump(array_map("cube", $num)); 输出:array(5) { [0]=> int(1) [1]=> int(4) [2]=> int(9) [3]=> int(16) [4]=> int(25) }
例子2(匿名函数也有效):<?php $func = function($n,$m){ return ("这个数组的第 $n 个位置的值是 $m <br>");};$a = array(1, 2, 3, 4, 5);$b = ['整型:1','整型:2','整型:3','整型:4','整型:5'];var_dump(array_map($func,$a,$b));输出:array(5) {[0]=> string(55) "这个数组的第 1 个位置的值是 整型:1 "[1]=> string(55) "这个数组的第 2 个位置的值是 整型:2 "[2]=> string(55) "这个数组的第 3 个位置的值是 整型:3 "[3]=> string(55) "这个数组的第 4 个位置的值是 整型:4 "[4]=> string(55) "这个数组的第 5 个位置的值是 整型:5 "}
注意第二个例子中的匿名函数里的语句必须使用双引号,因为其中包含$n,$m两个传参,而在PHP中,双引号内的字符会被分析器进行一遍解析,这样才能将其中的参数代表的值解析出来。
最后是从事数据开发时经常会碰到的问题,本人是在laravel框架下进行PHP开发,在利用其中的数据模型取数据时会出现一种情况,取出的数据是返回的对象类型(ObjClass),此时是无法直接利用键取出某一数据的,此时一般采用的方法是利用json_decode()方法,将其参数设为true时可返回数组类型的数据,但是此时返回的数组则是一个两层次的数组,就像
$arr = array( 0 => array( 'id' => 11111 ), 1 => array( 'id' => 22222 ), 2 => array( 'id' => 33333 ));
此时可以用foreach取出id,但是也可以用array_map()方法
$id_arr = array_map(function($v) { return $v['id'];}, $arr);输出:array(3) {[0]=> int(111) [1]=> int(222) [2]=> int(333) }
不过foreach遍历数组也很快,这两种方法就是看个人意愿了

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

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

SublimeText3 Linux new version
SublimeText3 Linux latest version