如何在没有索引的情况下提取数组的键和值 [英] How to extract the keys and values of an array without index

查看:99
本文介绍了如何在没有索引的情况下提取数组的键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数组(带有键和值的原始数组)中提取数据.提取数组后,我需要两个新数组,第一个数组仅包含键,第二个数组仅包含值,并且两个都没有索引(请参见代码示例).

I want to extract data from an array (original array with key and value). After I extract the array, I want two new arrays, the first one with just the keys, the second one with just the values, and both without indexes (see code example).

// 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);

好吧,您可以在此处阅读.

在计算机科学中,数组数据结构或简单地数组是 由元素(值或 变量),每个变量至少由一个数组索引或键标识.一个 存储数组,以便可以计算每个元素的位置 通过一个数学公式从其索引元组中删除.

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.

这篇关于如何在没有索引的情况下提取数组的键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆