php – 如何在没有索引的情况下提取数组的键和值

我想从数组中提取数据(具有键和值的原始数组).在我提取数组之后,我想要两个新数组,第一个只有键,第二个只有值,两个都没有索引(参见代码示例).

// original array 
$array = array(
    "name1"=>500
   ,"name2"=>400
   ,"name3"=>300
   ,"name4"=>200
   ,"name5"=>100
);

// after extraction
$array1 = array('name1','name2','name3','name4','name5');
$array2 = array(500,400,300,200,100);

// not like this
// $array1 = array(0=>'name1',1=>'name2',2=>'name3',3=>'name4',4=>'name5);
// $array2 = array(0=>500,1=>400,2=?300,3=>200,4=>100);

解决方法:

$array1 = array_keys($array);
$array2 = array_values($array);

好吧,你可以阅读here.

In computer science, an array data structure or simply an array is a
data structure consisting of a collection of elements (values or
variables), each identified by at least one array index or key
. An
array is stored so that the position of each element can be computed
from its index tuple by a mathematical formula.

上一篇:如何用PHP提取动画GIF的帧


下一篇:在scrapy中利用Selector来提取数据