如何从第一个数组和第二个数组中获取特定值

时间:2014-01-21 07:21:57

标签: php arrays

如何使用php获取第一个数组和第二个数组中的纬度值?我希望你能教我怎么做..提前谢谢

这里是数组

Array
(
    [0] => Array
    (
        [unit_id] => 2
        [added_when] => 2014-01-10 09:57:05
        [latitude] => 11.6994922
        [longitude] => 122.36759245
        [speed] => 0
        [head] => 
        [valid] => 1
        [bearing] => 0
        [accuracy] => 141
        [altitude] => 0
        [distance] => 18
        [id] => 2014-01-10
        [start_datetime] => 2014-01-10 09:53:48
        [stop_time] => 18:38:05
        [client_id] => 2
        [imei] => 865687015810821
        [name] => cm flare
        [group_id] => 1
        [status] => 1
    )

    [1] => Array
    (
        [unit_id] => 2
        [added_when] => 2014-01-09 10:48:06
        [latitude] => 11.6994922
        [longitude] => 122.36759245
        [speed] => 0
        [head] => 
        [valid] => 1
        [bearing] => 0
        [accuracy] => 141
        [altitude] => 0
        [distance] => 0
        [id] => 2014-01-09
        [start_datetime] => 2014-01-09 10:45:46
        [stop_time] => 17:27:07
        [client_id] => 2
        [imei] => 865687015810821
        [name] => cm flare
        [group_id] => 1
        [status] => 1
    )
)

3 个答案:

答案 0 :(得分:2)

如果您有动态数组

$first = $array[0]['latitude'];
$last = $array[count($array)-1]['latitude'];

对于您的示例,您可以使用此代码

$first = $array[0]['latitude'];
$last = $array[1]['latitude'];

答案 1 :(得分:1)

如果您想要一种通用且可重复使用的方式来定义此函数:

function array_pluck ($toPluck, $arr) {
  return array_map(function ($item) use ($toPluck) {
  return $item[$toPluck];
  }, $arr);
}

然后

$latitudes = array_pluck("latitude", $myarrays);

答案 2 :(得分:0)

使用foreach循环。

foreach($arrays as $array) {
    echo $array['latitude'];
    echo $array['longitude'];
}