过滤大型PHP数组

时间:2013-02-02 17:54:27

标签: php arrays filter

此数据从Flickr API返回。它包含1000个带有照片细节的阵列。数组如下......

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
        )

);

我想过滤只包含latitude的数组,因为我编写了这个程序......

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

这是正确的方法吗?或者有更好的方法来过滤阵列?提前谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用array_filter功能:

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