Madan Sapkota
Madan Sapkota

Reputation: 26071

Filter large PHP array

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.

Upvotes: 2

Views: 413

Answers (1)

cdmckay
cdmckay

Reputation: 32240

You could use the array_filter function:

$latlngs = array_filter($items, function ($item) {
    return !in_array($item['latitude'], array('0', '', 'NULL')));
});

Upvotes: 1

Related Questions