1

I have an array that is taken from an image using

exif_read_data($image, 0, true)

The array itself can contain an unknown number of keys/values (can also be 0) The array is also Multidimentional in some parts.

An example of an array from exif_read_data :

Array
(
    [FILE] => Array
        (
            [FileName] => f-20110129_004_pp.jpg
            [FileDateTime] => 0
            [FileSize] => 3566966
            [FileType] => 2
            [MimeType] => image/jpeg
            [SectionsFound] => ANY_TAG, IFD0, THUMBNAIL, EXIF, GPS
        )

    [COMPUTED] => Array
        (
            [html] => width="2576" height="1936"
            [Height] => 1936
            [Width] => 2576
            [IsColor] => 1
            [ByteOrderMotorola] => 0
            [ApertureFNumber] => f/2.8
            [Thumbnail.FileType] => 2
            [Thumbnail.MimeType] => image/jpeg
        )

    [IFD0] => Array
        (
            [ImageWidth] => 2576
            [ImageLength] => 1936
            [BitsPerSample] => Array
                (
                    [0] => 8
                    [1] => 8
                    [2] => 8
                )

            [Make] => Nokia
            [Model] => N900
            [Orientation] => 1
            [SamplesPerPixel] => 3
            [XResolution] => 3000000/10000
            [YResolution] => 3000000/10000
            [ResolutionUnit] => 2
            [Software] => Adobe Photoshop CS5 Windows
            [DateTime] => 2011:01:29 09:37:30
            [YCbCrPositioning] => 1
            [Exif_IFD_Pointer] => 276
            [GPS_IFD_Pointer] => 658
        )

    [THUMBNAIL] => Array
        (
            [Compression] => 6
            [XResolution] => 72/1
            [YResolution] => 72/1
            [ResolutionUnit] => 2
            [JPEGInterchangeFormat] => 978
            [JPEGInterchangeFormatLength] => 5525
        )

    [EXIF] => Array
        (
            [ExposureTime] => 1/500
            [FNumber] => 14/5
            [ExposureProgram] => 0
            [ISOSpeedRatings] => 100
            [ExifVersion] => 0210
            [DateTimeOriginal] => 2011:01:29 09:37:30
            [DateTimeDigitized] => 2011:01:29 09:37:30
            [ShutterSpeedValue] => 8/1
            [ApertureValue] => 297/100
            [LightSource] => 0
            [Flash] => 0
            [FocalLength] => 26/5
            [FlashPixVersion] => 0100
            [ColorSpace] => 1
            [ExifImageWidth] => 2576
            [ExifImageLength] => 1936
            [CustomRendered] => 0
            [ExposureMode] => 0
            [WhiteBalance] => 0
            [DigitalZoomRatio] => 1/1
            [SceneCaptureType] => 0
            [GainControl] => 0
            [Contrast] => 0
            [Saturation] => 0
        )

    [GPS] => Array
        (
            [GPSVersion] => 
            [GPSLatitudeRef] => N
            [GPSLatitude] => Array
                (
                    [0] => 22/1
                    [1] => 12937/1000
                    [2] => 0/1
                )

            [GPSLongitudeRef] => E
            [GPSLongitude] => Array
                (
                    [0] => 113/1
                    [1] => 32886/1000
                    [2] => 0/1
                )

            [GPSAltitudeRef] => 
            [GPSAltitude] => 255/1
            [GPSTimeStamp] => Array
                (
                    [0] => 9/1
                    [1] => 37/1
                    [2] => 30/1
                )

            [GPSMapDatum] => WGS-84
            [GPSDateStamp] => 2011:01:29
        )

)

My question is how can I create a function that will display only the keys that I select , as a key/value pair , even if it is in the 2nd, or 3rd dimension of the array?

for example - from the array above , if I want to select only the [ImageWidth] , [ImageLength] , [XResolution] , [GPSTimeStamp] and [GPSLatitude] ..

I would pass it to the function like :

$keys_array = (ImageWidth , ImageLength, XResolution, GPSTimeStamp , GPSLatitude) 

and then

function select_keys_from_array ($keys_array='') {  
// if $keys_array=='' then get all ..
//identify the dimension or flatten - and get only my keys and display key/value 
}

I have selected those keys as example because some of them are on a second-level, and some are actually arrays themselves ..

There is also a problem is that the keys can theoretically be duplicated (user-keys) - but residing in different second-level array (and therefor not nominally duplicated.)

I guess I will need to "flatten" it first , and then to "pass" an array of my wanted keys somehow - but I can not really seem to get it right .

Does someone knows any ready-made class / function / snippet for that sort of thing ?

Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105
  • You should use a function that loops through the tree recursively. The condition of return it should be located recurisvidad the key passed as parameter. – Lobo Mar 28 '12 at 07:20

4 Answers4

1

Write a recursive function that converts a multidimensional array to a flat one and eliminate duplicate keys, or those you don't want.

function multi2flat($array) 
{
    $return = array();
    array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
    return $return;
}
slash197
  • 9,028
  • 6
  • 41
  • 70
1

You don't necessarily need to flatten it - in fact, doing so may overwrite those keys you mentioned that might appear in more than one sub-array. You just need to be able to successfully walk the array, including nested arrays, using recursion (the routine would read a single array from start to finish, but would call itself recursively for every sub-array it encounters). Once you can walk it like this, then you can simply compare the keys you are encountering against the ones you want.

If you want specific versions of keys that appear in multiple places then you are going to have to qualify them ('scope' them) somehow - for example, using COMPUTED.Height rather than just Height. Your walking algorithm will have to keep track of the path through the array (i.e., the parent array chain it has walked to get that far) to allow this comparison.

JTeagle
  • 2,196
  • 14
  • 15
  • Thanks - Logically your answers makes a lot of sense (you put my chaotic thoughts into words) - but technically I have no idea how to do that . Is there some ready-made class for something like that ? – Obmerk Kronen Mar 28 '12 at 07:26
  • 1
    It might not be as daunting as it sounds - foreach is a great way to walk the elements of an array. Check out this SO question / answer: http://stackoverflow.com/questions/26007/iterating-over-a-complex-associative-array-in-php – JTeagle Mar 28 '12 at 07:29
1

(Disclaimer; may do wacky stuff, not fully tested -- should be fine though)

Last edit; the first one was better, as it didn't exclude array values (such as coordinates, etc.)

function array_by_keys_recursive(array $keys, array $array) {
    $results = array();
    foreach ($keys as $key) {
        if (isset($array[$key])) {
            $results[$key] = $array[$key];
            continue;
        }
        foreach ($array as $value) {
            if (\is_array($value)) {
                $results = \array_replace($results,
                    \array_by_keys_recursive(array($search), $value));
            }
        }
    }
    return $results;
}

Test:

$array = array(
    'a' => 1,
    'b' => 2,
    'c' => array(
        'd' => 3,
        'e' => 4,
    ),
    'f' => 5,
    'g' => array(
        'h' => array(
            'i' => 6,
            'j' => 7,
        ),
        'k' => 8,
    ),
);

\var_dump(\array_by_keys_recursive(array('a', 'b', 'c', 'h', 'i', 'j'), $array));

Results:

array(6) {
  ["a"]=>
  int(1)
  ["b"]=>
  int(2)
  ["c"]=>
  array(2) {
    ["d"]=>
    int(3)
    ["e"]=>
    int(4)
  }
  ["h"]=>
  array(2) {
    ["i"]=>
    int(6)
    ["j"]=>
    int(7)
  }
  ["i"]=>
  int(6)
  ["j"]=>
  int(7)
}
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
  • ok, that seems to work, but what about possible duplicated keys ? – Obmerk Kronen Mar 28 '12 at 07:47
  • @ObmerkNinenine There ya go! It'll take the last "duplicate key" (*across the whole array*) it finds. – Dan Lugg Mar 28 '12 at 07:55
  • hmm.. now I got confused :-) the former ones looked like they are doing the job - unfortunately i am on an old local server - so the last one gives me undefined function on array_replace'() - but looking at your example - it still returns some keys as arrays when they are on a second / third level .. – Obmerk Kronen Mar 28 '12 at 08:05
1
<?

$x = Array
(
    'FILE' => Array
        (
            'FileName' => 'f-20110129_004_pp.jpg',
            'FileDateTime' => 0,
            'FileSize' => 3566966,
            'FileType' => 2,
            'MimeType' => 'image/jpeg',
            'SectionsFound' => 'ANY_TAG, IFD0, THUMBNAIL, EXIF, GPS',
        ),

    'COMPUTED' => Array
        (
            'html' => 'width="2576" height="1936"',
            'Height' => 1936,
            'Width' => 2576,
            'IsColor' => 1,
            'ByteOrderMotorola' => 0,
            'ApertureFNumber' => 'f/2.8',
            'Thumbnail.FileType' => 2,
            'Thumbnail.MimeType' => 'image/jpeg',
        ),

    'IFD0' => Array
        (
            'ImageWidth' => 2576,
            'ImageLength' => 1936,
            'BitsPerSample' => Array
                (
                    '0' => 8,
                    '1' => 8,
                    '2' => 8,
                ),

            'Make' => 'Nokia',
            'Model' => 'N900',
            'Orientation' => 1,
            'SamplesPerPixel' => 3,
            'XResolution' => '3000000/10000',
            'YResolution' => '3000000/10000',
            'ResolutionUnit' => 2,
            'Software' => 'Adobe Photoshop CS5 Windows',
            'DateTime' => '2011:01:29 09:37:30',
            'YCbCrPositioning' => 1,
            'Exif_IFD_Pointer' => 276,
            'GPS_IFD_Pointer' => 658,
        ),

    'THUMBNAIL' => Array
        (
            'Compression' => 6,
            'XResolution' => '72/1',
            'YResolution' => '72/1',
            'ResolutionUnit' => 2,
            'JPEGInterchangeFormat' => 978,
            'JPEGInterchangeFormatLength' => 5525,
        ),

    'EXIF' => Array
        (
            'ExposureTime' => '1/500',
            'FNumber' => '14/5',
            'ExposureProgram' => 0,
            'ISOSpeedRatings' => 100,
            'ExifVersion' => '0210',
            'DateTimeOriginal' => '2011:01:29 09:37:30',
            'DateTimeDigitized' => '2011:01:29 09:37:30',
            'ShutterSpeedValue' => '8/1',
            'ApertureValue' => '297/100',
            'LightSource' => 0,
            'Flash' => 0,
            'FocalLength' => '26/5',
            'FlashPixVersion' => '0100',
            'ColorSpace' => 1,
            'ExifImageWidth' => 2576,
            'ExifImageLength' => 1936,
            'CustomRendered' => 0,
            'ExposureMode' => 0,
            'WhiteBalance' => 0,
            'DigitalZoomRatio' => '1/1',
            'SceneCaptureType' => 0,
            'GainControl' => 0,
            'Contrast' => 0,
            'Saturation' => 0,
        ),

    'GPS' => Array
        (
            'GPSVersion' => '',
            'GPSLatitudeRef' => 'N',
            'GPSLatitude' => Array
                (
                    '0' => '22/1',
                    '1' => '12937/1000',
                    '2' => '0/1',
                ),

            'GPSLongitudeRef' => 'E',
            'GPSLongitude' => Array
                (
                    '0' => '113/1',
                    '1' => '32886/1000',
                    '2' => '0/1',
                ),

            'GPSAltitudeRef' => '',
            'GPSAltitude' => '255/1',
            'GPSTimeStamp' => Array
                (
                    '0' => '9/1',
                    '1' => '37/1',
                    '2' => '30/1',
                ),

            'GPSMapDatum' => 'WGS-84',
            'GPSDateStamp' => '2011:01:29',
        ),

);

function get_values( $data, $keys ) {
    $ret = Array();
    foreach( $data as $k => $v ) {
        if( is_array( $v ) ) {
            $t = get_values( $v, $keys );
            if( is_array( $t ) && sizeOf( $t ) > 0 ) {
                $ret[$k] = $t;
            }
        } else {
            if( in_array( $k, $keys ) ) {
                $ret[ $k ] = $v;
            }
        }
    }
    return $ret;
}

print_r( get_values( $x, Array( 'ImageWidth', 'ImageLength', 'XResolution', 'GPSLatitude' ) ) );

?>

devanand
  • 5,116
  • 2
  • 20
  • 19