jttorate
jttorate

Reputation: 61

How do i get specific value from first array and second array

How i can get value of latitude in first array and second array using php? I hope you can teach me how to do it.. Thanks in advance

Here the array

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

Upvotes: 0

Views: 69

Answers (3)

Cranio
Cranio

Reputation: 9847

if you want a general and reusable way define this function:

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

then

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

Upvotes: 1

Liauchuk Ivan
Liauchuk Ivan

Reputation: 1993

If you has a dynamic array

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

For you example you can just use this code

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

Upvotes: 2

chanchal118
chanchal118

Reputation: 3647

Use a foreach loop.

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

Upvotes: 0

Related Questions