0

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
    )
)
1
  • You have an array of arrays. $array[0] means first array in your main array, assuming your main array is set as a variable named $array. Commented Jan 21, 2014 at 7:30

3 Answers 3

2

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'];
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

Use a foreach loop.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.