帮我找一个php函数
印象中PHP有这么一个函数:
$a = 1;$b = 2;$array = xxxx($a,$b);
结果返回的数组为
array(
'a' => 1,
'b' => 2
)
也就是把变量名称作为键名
每当遇到这种问题,我给搜索引擎跪了~
回复讨论(解决方案)
你为什么不直接这样,就不需要那个函数了
$arr = array();
$arr['a'] = 1;
$arr['b'] = 2;
你为什么不直接这样,就不需要那个函数了
$arr = array();
$arr['a'] = 1;
$arr['b'] = 2;
找到了
至于为什么~
$a = 1;$b = 2;$c = 3;$array1 = compact('a', 'b', 'c');$array2 = array('a'=>$a, 'b'=>$b, 'c'=>$c);
你觉得哪个比较好写
该是数组的,一开始就没必要初始化成变量。
$a = 1;
$b = 2;
$c = 3;
$array1 = compact('a', 'b', 'c');
这样用了4行
$array2 = array('a'=>1, 'b'=>2, 'c'=>3);
这样只需要一行
$arr = array();
$arr['a'] = 1;
$arr['b'] = 2;
$arr['c'] = 3;
即使是这样,也是4行没差。
需要的时候用$arr['b']这样读就行了,根本不需要$b这个变量
除非$arr['b']['c']['d'] 这样很长,又多次使用的时候才存一个变量简化一下代码。
但是不得不承认确实忘了有compact这个函数
该是数组的,一开始就没必要初始化成变量。
$a = 1;
$b = 2;
$c = 3;
$array1 = compact('a', 'b', 'c');
这样用了4行
$array2 = array('a'=>1, 'b'=>2, 'c'=>3);
这样只需要一行
$arr = array();
$arr['a'] = 1;
$arr['b'] = 2;
$arr['c'] = 3;
即使是这样,也是4行没差。
需要的时候用$arr['b']这样读就行了,根本不需要$b这个变量
除非$arr['b']['c']['d'] 这样很长,又多次使用的时候才存一个变量简化一下代码。
前面三个变量那是前提条件啊,都只是一行而已吧
目的是把变量组成数组,就为了这个数组,你会去改变前边已有的源码吗。要是过程繁杂,都懒得看。只知道结果有一些变量,现在要组成数组。否则当然直接写成数组啦,谁没事找事做啊
$a = 1;$b = 2;$c = 3;$t = array_diff_key(get_defined_vars(), array_flip(array('_GET', '_POST', '_COOKIE', '_FILES')));print_r($t);
Array( [a] => 1 [b] => 2 [c] => 3)
$a = 1;$b = 2;$c = 3;$t = array_diff_key(get_defined_vars(), array_flip(array('_GET', '_POST', '_COOKIE', '_FILES')));print_r($t);
Array( [a] => 1 [b] => 2 [c] => 3)
够暴力
其实你用 compact 是有前提的:至少需要知道变量名
用 array_combine de 的话,则有可能打乱原有的成员顺序
而 get_defined_vars 则可用于探测未知变量

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

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

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
