Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 帮我找一个php函数

帮我找一个php函数

Jun 20, 2016 pm 12:25 PM

印象中PHP有这么一个函数:

1

$a = 1;$b = 2;$array = xxxx($a,$b);

Copy after login

结果返回的数组为
array(
'a' => 1,
'b' => 2
)
也就是把变量名称作为键名

每当遇到这种问题,我给搜索引擎跪了~



回复讨论(解决方案)

你为什么不直接这样,就不需要那个函数了
$arr = array();
$arr['a'] = 1;
$arr['b'] = 2;

你为什么不直接这样,就不需要那个函数了
$arr = array();
$arr['a'] = 1;
$arr['b'] = 2;


找到了
至于为什么~

1

$a = 1;$b = 2;$c = 3;$array1 = compact('a', 'b', 'c');$array2 = array('a'=>$a, 'b'=>$b, 'c'=>$c);

Copy after login

你觉得哪个比较好写

该是数组的,一开始就没必要初始化成变量。

$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'] 这样很长,又多次使用的时候才存一个变量简化一下代码。


前面三个变量那是前提条件啊,都只是一行而已吧
目的是把变量组成数组,就为了这个数组,你会去改变前边已有的源码吗。要是过程繁杂,都懒得看。只知道结果有一些变量,现在要组成数组。否则当然直接写成数组啦,谁没事找事做啊

1

$a = 1;$b = 2;$c = 3;$t = array_diff_key(get_defined_vars(), array_flip(array('_GET', '_POST', '_COOKIE', '_FILES')));print_r($t);

Copy after login
Copy after login

1

Array(    [a] => 1    [b] => 2    [c] => 3)

Copy after login
Copy after login

1

$a = 1;$b = 2;$c = 3;$t = array_diff_key(get_defined_vars(), array_flip(array('_GET', '_POST', '_COOKIE', '_FILES')));print_r($t);

Copy after login
Copy after login

1

Array(    [a] => 1    [b] => 2    [c] => 3)

Copy after login
Copy after login


够暴力

其实你用 compact 是有前提的:至少需要知道变量名
用 array_combine de 的话,则有可能打乱原有的成员顺序
而 get_defined_vars 则可用于探测未知变量

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles