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.