1.request_uri获取到最后的元素index\test1\test2\test3(一般框架的的路由路径就是这样的)
$request_uri ="index\\test1\\test2\\test3";$arr=explode("\\",$request_uri);$moudle = array_shift($arr);$control = array_shift($arr);$action = array_shift($arr);$parms= array_shift($arr);
这样减少了我们常见的一种比较死板的写法,$control = $arr[1]
这种写法
2.我们获取到了某个读取框架内的某个路径控制器的某个行为还有参数,我们不希望外部看到我们内部的文件和控制器路径,对每个路径进行转换处理,我们希望对外输出的路由是友好的
$changeMoudle ="Change".array_shift($arr);$changeController = "Change".array_shift($arr);//然后我们让其组合到返回的url里面去array_unshift($arr,$changeController);array_unshift($arr,$changeMoudle);implode($arr,"//");
3.数组合并的方法array_merge 合并增强的方法
array_merge_recursive($arr1,$arr2)递归合并 一般在数组配置的时候,我们会用另外一种方法
下面给出一个常用的配置逻辑方法:(用后来的配置覆盖掉前面的配置)
function arrayRecusive($arr1,$arr2) { if(is_array($arr2)) { foreach($arr2 as $k=>$v){ if(is_array($v)) { $arr1[$k]=arrayRecusive($arr1[$k],$v); } else { $arr1[$k]=$v; } } } return $arr1;}
4.array_key_exists()和in_array()
这俩个函数在初期的时候,不留神的时候,容易写错。
一个是检测是不是键值存在,另外一个是检测是不是值存在
应用场景
检测对应的配置是不是已经被加载过(如果加载过,就不必重新加载)
array_key_exists("key",$arr)
5.array_push($arr,$test);
将变量$test压入到$arr内部 用法和$arr[]=$test;差不多
压入一个值,也是常用的功能
对应的一个array_pop($arr)弹出压入的最后一个值
6.list 将一个数组赋值给变量
场景:用.切割了microtime(true)后,需要将前部分和后部分进行分开储存
list($t1,$t2) =explode(".",microtime(true))
7.shuffle($arr)将数组打乱后输出
获取到新闻和广告后,需要使得每个用户看到的广告位置不同(在一般的资讯站点里面,每个页面放广告的位置都不同,用这种方法来提高点击率常用)
8.array_map("ucfirst",$arr);
回调函数(这个是经常用的)对$arr进行处理,每个字符串进行首字母大写
需要调用实例化后的类,而类第一个字母通常是大写,这样可以对$arr内的所有元素进行首字母大写(返回作用后的大小写字母)
相似的一个函数有array_walk($arr,"test");(正确区分这俩个函数难度其实是比较大的)
array_walk($arr,"test") 是过程里面变动的调用。下面对示例进行说明:
$arr = array("xiong","yuan","liang");function test($val){ echo ucfirst($val)."<br />";}function test1($val){ return ucfirst($val)."<br />"; }$arr1= array_walk($arr,"test1");$arr2 = array_walk($arr,"test");print_r($arr);echo "<br />";$arr3 = array_map("test1",$arr);print_r($arr3);
小结:array_map返回的是回调函数的结果,而array_walk返回的是是否回调成功true或者false
如果不用&,俩者都不会改变数组
更形象的用法:array_walk($arr,"test")是可以用下面这个方法进行替代的
foreach($arr as $key=>$val) {
test($val,$key);
}
9.sort($arr) 会对数组排序
默认是按照自然排序的 一般用其变种函数usort($arr,"test")比较多,test是用户自定义的函数,可以按照指定的要求进行排序
test的返回值需要大于等于0,或者小于0(usort根据这个进行排序)
这里引用一个非常经典的测试:(二维数组的排序,实际上多维数组的排序也差不多,这里假定了根据每个二维数组的第二个键值进行比较)
function comp($arr1,$arr2){ return strcmp($arr1[1],$arr2[1]); } usort($arr,"comp");
另外有一个PHP方法可以直接对二维数组进行排序 array_multisort($arr)
10.array_change_key_case()
这个也是一个比较常用的PHP数组函数
应用场景:
用户输入的时候,可能没有注意大小写,或者将Linux和Windows的统一字符串命名的时候,经常需要进行字符串统一
默认情况下是将输入的数组进行全部转化成小写
array array_change_key_case ( array $input [, int $case = CASE_LOWER ] )

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

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

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

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

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

Dreamweaver Mac version
Visual web development tools