foucquet Posted October 26, 2011 Share Posted October 26, 2011 I have an array ( $exifdata ) which contains the relevant bits that I require, read from exif data, as in the var_dump below. array 0 => string 'Caption' (length=7) 1 => string 'Copy' (length=4) 2 => string 'Camera' (length=6) 3 => string 'Shutter' (length=7) 4 => string 'fNo' (length=3) 5 => string 'ISO' (length=3) 6 => string 'Date' (length=4) 7 => string 'Time' (length=4) 'copy' => string 'Alf Thomas' (length=10) 'caption' => string 'Blue Sky over Auld Reekie' (length=25) 'camera' => string 'Canon PowerShot G9' (length=18) 'shutter' => string '1/800' (length=5) 'fNo' => string 'f/5.6' (length=5) 'iso' => int 200 'date' => string '07:10:2011' (length=10) 'time' => string '09:05:26' (length=8 What I am trying to do is echo each key followed by its relevant data, and thought something like this would work:- foreach( $exifdata as $key => $value){ echo "$key :, $value <br />"; } The output I get from the above is as below:- 0 :, Caption 1 :, Copy 2 :, Camera 3 :, Shutter 4 :, fNo 5 :, ISO 6 :, Date 7 :, Time copy :, Foucquet caption :, Blue Sky over Auld Reekie camera :, Canon PowerShot G9 shutter :, 1/800 fNo :, f/5.6 iso :, 200 date :, 07:10:2011 time :, 09:05:26 Which more or less does what I want except that what I actually would like is this:- Copy - Foucquet Caption - Blue Sky over Auld Reekie Camera - Canon PowerShot G9 Shutter - 1/800 fNo - f/5.6 ISO - 200 Date - 07:10:2011 Time - 09:05:26 What I can't quite understand is why I am getting the two lists as an output, which simply seems to be identical to the output of the var_dump. Link to comment https://forums.phpfreaks.com/topic/249830-echo-array-key-with-its-relevant-data/ Share on other sites More sharing options...
Buddski Posted October 26, 2011 Share Posted October 26, 2011 Your array has 8 numeric keys and 8 associative keys. You are getting that output because your array contains all of those 16 elements (as the var_dump states) Will your array always contain these 16 elements? Link to comment https://forums.phpfreaks.com/topic/249830-echo-array-key-with-its-relevant-data/#findComment-1282337 Share on other sites More sharing options...
foucquet Posted October 26, 2011 Author Share Posted October 26, 2011 I see that now that you point it out, what it should be is an associative array with no numerical keys at all, I have obviously done something stupid here:- $exifdata = array('Caption', 'Copy', 'Camera', 'Shutter', 'fNo', 'ISO', 'Date', 'Time'); //>> Populate $exifdata if (array_key_exists('Copyright', $exif)) { $exifdata['copy'] = $exif['Copyright']; } else { $exifdata['copy'] = "Foucquet"; } $exifdata['caption'] = $exif['COMMENT'][0]; $exifdata['camera'] = $exif['Model']; $exifdata['shutter'] = $exif['ExposureTime']; $exifdata['fNo'] = $exif['COMPUTED']['ApertureFNumber']; $exifdata['iso'] = $exif['ISOSpeedRatings']; $exifdata['date'] = $date; $exifdata['time'] = $time; but am not quite sure what at this point - any ideas? Link to comment https://forums.phpfreaks.com/topic/249830-echo-array-key-with-its-relevant-data/#findComment-1282341 Share on other sites More sharing options...
Buddski Posted October 26, 2011 Share Posted October 26, 2011 This line $exifdata = array('Caption', 'Copy', 'Camera', 'Shutter', 'fNo', 'ISO', 'Date', 'Time'); is creating the numerical keys with those values assigned. With the rest of your code in place you can just replace that line with $exifdata = array(); Link to comment https://forums.phpfreaks.com/topic/249830-echo-array-key-with-its-relevant-data/#findComment-1282347 Share on other sites More sharing options...
foucquet Posted October 26, 2011 Author Share Posted October 26, 2011 I just knew I had done summat stupid - so bleeding obvious! Thanks Buddski Link to comment https://forums.phpfreaks.com/topic/249830-echo-array-key-with-its-relevant-data/#findComment-1282349 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.