Help me find a PHP function

Source: Internet
Author: User
The impression that PHP has such a function:
$a = 1; $b = 2; $array = xxxx ($a, $b);

The result returns an array of
Array
' A ' = 1,
' B ' = 2
)
That is, the variable name as the key name

Whenever I encounter this problem, I kneel to the search engine ~



Reply to discussion (solution)

Why don't you just do that, you don't need that function.
$arr = Array ();
$arr [' a '] = 1;
$arr [' b '] = 2;

Why don't you just do that, you don't need that function.
$arr = Array ();
$arr [' a '] = 1;
$arr [' b '] = 2;


Got it
As for why ~
$a = 1; $b = 2; $c = 3; $array 1 = compact (' A ', ' B ', ' C '); $array 2 = Array (' a ' + = $a, ' b ' + = $b, ' c ' = = $c);

Which one do you think is better to write?

This is an array, and it is not necessary to initialize the variables at the outset.

$a = 1;
$b = 2;
$c = 3;
$array 1 = Compact (' A ', ' B ', ' C ');
That's 4 lines.

$array 2 = Array (' A ' =>1, ' B ' =>2, ' C ' =>3);
This requires only one line

$arr = Array ();
$arr [' a '] = 1;
$arr [' b '] = 2;
$arr [' c '] = 3;
Even so, 4 rows is not bad.

It's OK to read it with $arr[' B ' when you need it, and you don't need to $b this variable at all.
Save a variable to simplify the code unless $arr[' B ' [' C '] [' d '] is a long and multiple use.

But I have to admit that we did forget the compact.

This is an array, and it is not necessary to initialize the variables at the outset.

$a = 1;
$b = 2;
$c = 3;
$array 1 = Compact (' A ', ' B ', ' C ');
That's 4 lines.

$array 2 = Array (' A ' =>1, ' B ' =>2, ' C ' =>3);
This requires only one line

$arr = Array ();
$arr [' a '] = 1;
$arr [' b '] = 2;
$arr [' c '] = 3;
Even so, 4 rows is not bad.

It's OK to read it with $arr[' B ' when you need it, and you don't need to $b this variable at all.
Save a variable to simplify the code unless $arr[' B ' [' C '] [' d '] is a long and multiple use.


Previous three variables that's the precondition. Ah, it's just a line.
The goal is to make the variable into an array, just for this array, you will change the existing source code in front of it. If the process is complicated, do not bother to look. Only know the result has some variables, now to form an array. Otherwise, of course, directly into the array, who has nothing to do?

$a = 1; $b = 2; $c = 3; $t = Array_diff_key (Get_defined_vars (), Array_flip (Array (' _get ', ' _post ', ' _cookie ', ' _files '));p Rin T_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 '));p Rin T_r ($t);
Array (    [A] = 1    [b] = 2    [c] = 3)


Enough violence.

In fact, you use the compact is a prerequisite: at least need to know the variable name
With Array_combine de, it is possible to disrupt the original member order
And get_defined_vars can be used to detect unknown variables.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.