2

This data is return from Flickr API. It contains 1000 arrays with photo details. The array is given below...

Array
(
    [0] => Array
        (
            [id] => 8437769421
            [owner] => 10817753@N03
            [secret] => 9eccf2d244
            [server] => 8056
            [farm] => 9
            [title] => [Group 1]-XR6A1835_XR6A1840-6 images-E.jpg
            [ispublic] => 1
            [isfriend] => 0
            [isfamily] => 0
            [description] => Array
                (
                    [_content] => Stitched Panorama
                )

            [dateupload] => 1359826577
            [datetaken] => 2012-12-24 10:32:04
            [datetakengranularity] => 0
            [ownername] => Ulf Bodin
            [tags] => mist fog landscape spain ray rays andalusia andalusien spanien landskap salobreña dimma canoneos5dmarkiii canonef70200mmf28lisiiusm
            [latitude] => 36.743055
            [longitude] => -3.588651
            [accuracy] => 16
            [context] => 0
            [place_id] => qvlfaYpWVbiHFTA
            [woeid] => 772523
            [geo_is_family] => 0
            [geo_is_friend] => 0
            [geo_is_contact] => 0
            [geo_is_public] => 1
        )

    [1] => Array
        (
            [id] => 8437770275
            [owner] => 69309429@N02
            [secret] => 1805477eb0
            [server] => 8078
            [farm] => 9
            [title] => #Newfoundlanddogs #squirellhatersclub #landseer #newfie #dogs #newfiesofinstagram #instadogs #drool #newfiesarethebestdogsever
            [ispublic] => 1
            [isfriend] => 0
            [isfamily] => 0
            [description] => Array
                (
                    [_content] => 
                )

            [dateupload] => 1359826595
            [datetaken] => 2013-02-02 09:36:35
            [datetakengranularity] => 0
            [ownername] => chefwaiterhater
            [tags] => square squareformat normal iphoneography instagramapp uploaded:by=instagram
            [latitude] => 0
            [longitude] => 0
            [accuracy] => 0
            [context] => 0
        )
        .....
        .....
        .....
    [999] => Array
        (
            [id] => 8438855962
            [owner] => 23123736@N00
            [secret] => ab5b4dbf4d
            [server] => 8092
            [farm] => 9
            [title] => Dusky drama - drive home
            [ispublic] => 1
            [isfriend] => 0
            [isfamily] => 0
            [description] => Array
                (
                    [_content] => 
                )

            [dateupload] => 1359826602
            [datetaken] => 2013-02-02 17:36:42
            [datetakengranularity] => 0
            [ownername] => angee09
            [tags] => square squareformat iphoneography instagramapp xproii uploaded:by=instagram
            [latitude] => 0
            [longitude] => 0
            [accuracy] => 0
            [context] => 0
        )

);

I want to filter array only containing latitude for that I write this program...

function onlyLatLng($items)
{
    $filters = array();

    foreach ($items as $item) {
        if (!in_array($item['latitude'], array('0', '', 'NULL'))) {
            $filters[] = $item;
        }
    }
    return $filters;
}

$latlngs = onlyLatLng($above_big_array);

Is this right way to do? or there is better way to filter the array? Thanks in advance.

4
  • There is nothing wrong with the method you used. Commented Feb 2, 2013 at 17:57
  • This is fine. It can also be done with array_filter() and a callback similar to what you used in your loop. They will be comparably performant. Your method is clear and readable so I would probably stick with it. Commented Feb 2, 2013 at 17:58
  • J A, I have no problem with output, it is correct. but just want to know if there is better way to filter. Commented Feb 2, 2013 at 17:59
  • You're actually checking if the latitude key contains the string value "NULL", which is probably not what you mean to do. Commented Feb 2, 2013 at 18:01

1 Answer 1

1

You could use the array_filter function:

$latlngs = array_filter($items, function ($item) {
    return !in_array($item['latitude'], array('0', '', 'NULL')));
});
Sign up to request clarification or add additional context in comments.

1 Comment

empty() should be usable, except if the 'NULL' string is really intended as a string.

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.