从多维数组的列创建简单数组

时间:2013-03-01 14:56:17

标签: php arrays

如果我有一个包含某些mysql表数据的多维数组,例如:

Array ( [0] => Array ( [0] => 5255071 [id] => 5255071 [1] => 2013-02-28 20:40:48 [sent] => 2013-02-28 20:40:48 ) [1] => Array ( [0] => 5253758 [id] => 5253758 [1] => 2010-11-16 12:56:39 [sent] => 2010-11-16 12:56:39 ) [2] => Array ( [0] => 5253517 [id] => 5253517 [1] => 2010-11-16 12:43:57 [sent] => 2010-11-16 12:43:57 ) [3] => Array ( [0] => 5252348 [id] => 5252348 [1] => 2010-11-16 11:19:35 [sent] => 2010-11-16 11:19:35 ) [4] => Array ( [0] => 5252343 [id] => 5252343 [1] => 2010-11-16 11:19:01 [sent] => 2010-11-16 11:19:01 ) [5] => Array ( [0] => 5252254 [id] => 5252254 [1] => 2010-11-16 11:11:26 [sent] => 2010-11-16 11:11:26 ) [6] => Array ( [0] => 5251390 [id] => 5251390 [1] => 2010-11-16 09:16:57 [sent] => 2010-11-16 09:16:57 ) [7] => Array ( [0] => 5249381 [id] => 5249381 [1] => 2010-11-16 03:01:32 [sent] => 2010-11-16 03:01:32 ) [8] => Array ( [0] => 5249086 [id] => 5249086 [1] => 2010-11-16 02:47:30 [sent] => 2010-11-16 02:47:30 ) [9] => Array ( [0] => 5247987 [id] => 5247987 [1] => 2010-11-16 01:59:34 [sent] => 2010-11-16 01:59:34 ) [10] => Array ( [0] => 5247325 [id] => 5247325 [1] => 2010-11-16 01:33:12 [sent] => 2010-11-16 01:33:12 ) ) 

有没有办法创建一个只包含'id'值的新单个数组?这是:

array(5255071, 5253758, 5253517, 5252348, ...)

这个想法是避免使用foreach循环。提前谢谢。

2 个答案:

答案 0 :(得分:3)

您可以使用array_map()

function extractId ($array) { return $array['id']; }
$targetArray = array_map($sourceArray, "extractId");

答案 1 :(得分:1)

$output = array_values($array['id']);

请参阅:array_values