从2D阵列传输密钥以填充一维阵列

时间:2021-03-05 21:33:01

PHP has plenty of useful functions and Im wondering if Im overlooking one that has already been built.

PHP有很多有用的功能,我想知道我是否可以忽略已经构建的功能。

Lets say you have an array such as:

假设您有一个数组,例如:

$first_array = array("Name"=>"Angela", "Age"=>24);

and you wanted to grab the keys from the first array to create a second array (which could then be pushed into a third array). So you need to create:

并且你想从第一个数组中获取键以创建第二个数组(然后可以将其推入第三个数组)。所以你需要创建:

$second_array = array("Name", "Age");

Is there a way to achieve this result without this loop?:

有没有办法在没有这个循环的情况下实现这个结果?:

foreach($first_array as $k=>$v){
    array_push($second_array, $k);
}

2 个解决方案

#1


1  

This should do it:

这应该这样做:

array_keys($first_array);

#2


0  

Use array_keys($first_array) to get the array of all the keys in the $first_array

使用array_keys($ first_array)获取$ first_array中所有键的数组

#1


1  

This should do it:

这应该这样做:

array_keys($first_array);

#2


0  

Use array_keys($first_array) to get the array of all the keys in the $first_array

使用array_keys($ first_array)获取$ first_array中所有键的数组