I have a multidimensional array that I want to be regrouped. The array looks like the following.
$originalArray = array
(
'APPLE' => array(
'BASE_PRICE' => 7.5,
'MARKUP' => 1.2
),
'ORANGE' => array
(
'BASE_PRICE' => 0,
'MARKUP' => 4
),
'BANANA' => array
(
'BASE_PRICE' => 0,
'MARKUP' => 6
),
'LIME' => array
(
'BASE_PRICE' => 0,
'MARKUP' => 8
)
);
I want it reordered such that it is grouped into BASE_PRICE and MARKUP as the following.
$newArray = array (
'BASE_PRICE' => array
(
'APPLE' => 7.5,
'ORANGE' => 0,
'BANANA' => 0,
'LIME' => 0
),
'MARKUP' => array
(
'APPLE' => 1.2,
'ORANGE' => 6,
'BANANA' => 4,
'LIME' => 8
)
);
How do I use loops to achieve the new array? Any help would be greatly appreciated.
So far I've tried the following but not getting what I expect.
arsort($originalArray);
You can achieve the desired array by using
foreach ($originalArray as $key => $value) {
$array1['BASE_PRICE'][$key] = $value['BASE_PRICE'];
$array2['MARKUP'][$key] = $value['MARKUP'];
}
$newarray = array_merge($array1,$array2);
Output
Array
(
[BASE_PRICE] => Array
(
[APPLE] => 7.5
[ORANGE] => 0
[BANANA] => 0
[LIME] => 0
)
[MARKUP] => Array
(
[APPLE] => 1.2
[ORANGE] => 4
[BANANA] => 6
[LIME] => 8
)
)
you can use asort function :
function sort_array ($inputArray){
$outputArray = array();
foreach ($inputArray as $index => $value){
$outputArray["BASE_PRICE"][$index] = $value["BASE_PRICE"];
$outputArray["MARKUP"][$index] = $value["MARKUP"];
}
foreach ($outputArray as $value){
asort($value);
asort($value);
}
return $outputArray;
}
var_dump(sort_array($originalArray));
You can use array-reduce function by next way:
<?php
$originalArray = array
(
'APPLE' => array(
'BASE_PRICE' => 7.5,
'MARKUP' => 1.2
),
'ORANGE' => array
(
'BASE_PRICE' => 0,
'MARKUP' => 4
),
'BANANA' => array
(
'BASE_PRICE' => 0,
'MARKUP' => 6
),
'LIME' => array
(
'BASE_PRICE' => 0,
'MARKUP' => 8
)
);
$resultArray = array_reduce(
array_keys($originalArray),
function($accumulator, $key) use($originalArray) {
$accumulator['BASE_PRICE'][$key]=$originalArray[$key]['BASE_PRICE'];
$accumulator['MARKUP'][$key]=$originalArray[$key]['MARKUP'];
return $accumulator;
},[]
);
print_r($resultArray);
?>
Related
I have an array of subarrays in the following format:
[
'a' => ['id' => 20, 'name' => 'chimpanzee'],
'b' => ['id' => 40, 'name' => 'meeting'],
'c' => ['id' => 20, 'name' => 'dynasty'],
'd' => ['id' => 50, 'name' => 'chocolate'],
'e' => ['id' => 10, 'name' => 'bananas'],
'f' => ['id' => 50, 'name' => 'fantasy'],
'g' => ['id' => 50, 'name' => 'football']
]
And I would like to group it into a new array based on the id field in each subarray.
array
(
10 => array
(
e => array ( id = 10, name = bananas )
)
20 => array
(
a => array ( id = 20, name = chimpanzee )
c => array ( id = 20, name = dynasty )
)
40 => array
(
b => array ( id = 40, name = meeting )
)
50 => array
(
d => array ( id = 50, name = chocolate )
f => array ( id = 50, name = fantasy )
g => array ( id = 50, name = football )
)
)
$arr = array();
foreach ($old_arr as $key => $item) {
$arr[$item['id']][$key] = $item;
}
ksort($arr, SORT_NUMERIC);
foreach($array as $key => $value){
$newarray[$value['id']][$key] = $value;
}
var_dump($newarray);
piece of cake ;)
The following code adapts #Tim Cooper’s code to mitigate Undefined index: id errors in the event that one of the inner arrays doesn’t contain an id:
$arr = array();
foreach($old_arr as $key => $item)
{
if(array_key_exists('id', $item))
$arr[$item['id']][$key] = $item;
}
ksort($arr, SORT_NUMERIC);
However, it will drop inner arrays without an id.
E.g.
$old_arr = array(
'a' => array ( 'id' => 20, 'name' => 'chimpanzee' ),
'b' => array ( 'id' => 40, 'name' => 'meeting' ),
'c' => array ( 'id' => 20, 'name' => 'dynasty' ),
'd' => array ( 'id' => 50, 'name' => 'chocolate' ),
'e' => array ( 'id' => 10, 'name' => 'bananas' ),
'f' => array ( 'id' => 50, 'name' => 'fantasy' ),
'g' => array ( 'id' => 50, 'name' => 'football' ),
'h' => array ( 'name' => 'bob' )
);
will drop the 'h' array completely.
You can also use Arrays::groupBy() from ouzo-goodies:
$groupBy = Arrays::groupBy($array, Functions::extract()->id);
print_r($groupBy);
And result:
Array
(
[20] => Array
(
[0] => Array
(
[id] => 20
[name] => chimpanzee
)
[1] => Array
(
[id] => 20
[name] => dynasty
)
)
[40] => Array
(
[0] => Array
(
[id] => 40
[name] => meeting
)
)
[50] => Array
(
[0] => Array
(
[id] => 50
[name] => chocolate
)
[1] => Array
(
[id] => 50
[name] => fantasy
)
[2] => Array
(
[id] => 50
[name] => football
)
)
[10] => Array
(
[0] => Array
(
[id] => 10
[name] => bananas
)
)
)
And here are the docs for Arrays and Functions.
Here is a function that will take an array as the first argument and a criteria (a string or callback function) as the second argument. The function returns a new array that groups the array as asked for.
/**
* Group items from an array together by some criteria or value.
*
* #param $arr array The array to group items from
* #param $criteria string|callable The key to group by or a function the returns a key to group by.
* #return array
*
*/
function groupBy($arr, $criteria): array
{
return array_reduce($arr, function($accumulator, $item) use ($criteria) {
$key = (is_callable($criteria)) ? $criteria($item) : $item[$criteria];
if (!array_key_exists($key, $accumulator)) {
$accumulator[$key] = [];
}
array_push($accumulator[$key], $item);
return $accumulator;
}, []);
}
Here is the given array:
$arr = array(
'a' => array ( 'id' => 20, 'name' => 'chimpanzee' ),
'b' => array ( 'id' => 40, 'name' => 'meeting' ),
'c' => array ( 'id' => 20, 'name' => 'dynasty' ),
'd' => array ( 'id' => 50, 'name' => 'chocolate' ),
'e' => array ( 'id' => 10, 'name' => 'bananas' ),
'f' => array ( 'id' => 50, 'name' => 'fantasy' ),
'g' => array ( 'id' => 50, 'name' => 'football' )
);
And examples using the function with a string and a callback function:
$q = groupBy($arr, 'id');
print_r($q);
$r = groupBy($arr, function($item) {
return $item['id'];
});
print_r($r);
The results are the same in both examples:
Array
(
[20] => Array
(
[0] => Array
(
[id] => 20
[name] => chimpanzee
)
[1] => Array
(
[id] => 20
[name] => dynasty
)
)
[40] => Array
(
[0] => Array
(
[id] => 40
[name] => meeting
)
)
[50] => Array
(
[0] => Array
(
[id] => 50
[name] => chocolate
)
[1] => Array
(
[id] => 50
[name] => fantasy
)
[2] => Array
(
[id] => 50
[name] => football
)
)
[10] => Array
(
[0] => Array
(
[id] => 10
[name] => bananas
)
)
)
Passing the callback is overkill in the example above, but using the callback finds its use when you pass in an array of objects, a multidimensional array, or have some arbitrary thing you want to group by.
Maybe it's worth to mention that you can also use php array_reduce function
$items = [
['id' => 20, 'name' => 'chimpanzee'],
['id' => 40, 'name' => 'meeting'],
['id' => 20, 'name' => 'dynasty'],
['id' => 50, 'name' => 'chocolate'],
['id' => 10, 'name' => 'bananas'],
['id' => 50, 'name' => 'fantasy'],
['id' => 50, 'name' => 'football'],
];
// Grouping
$groupedItems = array_reduce($items, function ($carry, $item) {
$carry[$item['id']][] = $item;
return $carry;
}, []);
// Sorting
ksort($groupedItems, SORT_NUMERIC);
print_r($groupedItems);
https://www.php.net/manual/en/function.array-reduce.php
Because of how PHP's sorting algorithm treats multidimensional arrays -- it sorts by size, then compares elements one at a time, you can actually use a key-preserving sort on the input BEFORE restructuring. In functional style programming, this means that you don't need to declare the result array as a variable.
Code: (Demo)
asort($array);
var_export(
array_reduce(
array_keys($array),
function($result, $k) use ($array) {
$result[$array[$k]['id']][$k] = $array[$k];
return $result;
}
)
);
I must say that functional programming is not very attractive for this task because the first level keys must be preserved.
Although array_walk() is more succinct, it still requires the result array to be passed into the closure as a reference variable. (Demo)
asort($array);
$result = [];
array_walk(
$array,
function($row, $k) use (&$result) {
$result[$row['id']][$k] = $row;
}
);
var_export($result);
I'd probably recommend a classic loop for this task. The only thing the loop needs to do is rearrange the first and second level keys. (Demo)
asort($array);
$result = [];
foreach ($array as $k => $row) {
$result[$row['id']][$k] = $row;
}
var_export($result);
To be completely honest, I expect that ksort() will be more efficient than pre-loop sorting, but I wanted to a viable alternative.
Php multidimensional array same key’s same value’s related total in
new array. I have an array of following mentioned. i need new array
as total qty of same item_id. anyone can help would be appreciate.
My Original Array is as following
Array
(
[a] => Array
(
[item] => Array
(
[item_id] => 1
)
[qty] => 0
),
[b] => Array
(
[item] => Array
(
[item_id] => 2
)
[qty] => 35
),
[c] => Array
(
[item] => Array
(
[item_id] => 2
)
[qty] => 15
),
[e] => Array
(
[item] => Array
(
[item_id] => 3
)
[qty] => 20
),
);
I want array Output like following :
Array(
[0] => Array (
[item_id] => 1,
[item_total_qty] => 0,
)
[1] => Array (
[item_id] => 2,
[item_total_qty] => 50,
)
[2] => Array (
[item_id] => 3,
[item_total_qty] => 20,
)
);
Hope it help
$arrays = array(
'a' => array(
'item' => array(
'item_id' => 1
),
'qty' => 0
),
'b' => array(
'item' => array(
'item_id' => 2
),
'qty' => 35
),
'c' => array(
'item' => array(
'item_id' => 2
),
'qty' => 15
),
'd' => array(
'item' => array(
'item_id' => 3
),
'qty' => 20
)
);
$result = array();
foreach ($arrays as $key => $array) {
if (is_array($result) && !empty($result)) {
foreach ($result as $key => $r) {
if ($r['item_id'] == $array['item']['item_id']) {
$result[$key]['item_total_qty'] += $array['qty'];
continue 2;
}
}
$result[] = array(
'item_id' => $array['item']['item_id'],
'item_total_qty' => $array['qty']);
} else {
$result[] = array(
'item_id' => $array['item']['item_id'],
'item_total_qty' => $array['qty']);
}
}
Simple foreach on your original table:
$sorted = array();
foreach ($original as $item) {
$id = $item['item']['item_id'];
$sorted[$id]['item_total_qty'] = $sorted[$id] ? $sorted[$id] + $item['qty'] : item['qty'];
$sorted[$id]['item_id'] = $id;
}
$sorted = array_values($sorted);
How may I count the times a key shows up in a multi-array?
In the below example, weight should return 2, and reps 4. Does PHP have a built-in function for this?
[0] => Array
(
[0] => Array
(
[weight] => 317.51474856007
[reps] => 10
)
[1] => Array
(
[weight] => 50
[reps] => 10
)
[2] => Array
(
[reps] => 10
)
[3] => Array
(
[reps] => 10
)
)
you can try something like this:
function counter($array){
$result = array();
foreach($array as $key=>$value){
foreach($value as $name => $val){
$result[$name][]=$val;
}
}
return array('all'=>$result,'count weight'=>count($result['weight']),'count reps'=>count($result['reps']));
}
$array= Array(
'0' => Array
(
'weight' => 317.51474856007,
'reps' => 10
),
'1' => Array
(
'weight' => 50,
'reps' => 10
),
'2' => Array
(
'reps' => 12
),
'3' => Array
(
'reps' => 10
)
);
$resp = counter($array);
var_dump($resp);
Try this one liner using array_walk_recursive():
Given your array, $arr
array_walk_recursive($arr, function($v, $k)use(&$count){if(array_key_exists($k, $count))$count[$k]++;else $count[$k]=1;},$count=array());
print_r($count);
See demo
or the old fashioned way:
$count = array();
foreach ($arr as $ar){
foreach ($ar as $k=>$v){
if (array_key_exists($k, $count)){
$count[$k]++;
}
else{
$count[$k] = 1;
}
}
}
print_r($count);
See demo
Output:
Array
(
[weight] => 2
[reps] => 4
)
you can try this:
$array= Array(
'0' => Array
(
'weight' => 317.51474856007,
'reps' => 10
),
'1' => Array
(
'weight' => 50,
'reps' => 10
),
'2' => Array
(
'reps' => 10
),
'3' => Array
(
'reps' => 10
)
);
$weight=0;
$reps=0;
//print_r($array);
foreach($array as $key=>$val){
if(isset($val['weight'])){
echo $val['weight']."<br>";
$weight++;
}
if(isset($val['reps'])){
echo $val['reps']."<br>";
$resp++;
}
}
echo $weight." ".$resp;
Ii everyone, I have an post data in array like this, I'm so confused how create the logic in controller:
POST Data:
Array
(
[nama_agenda] => blalala
[kilasan] => asdsadsadasd
[tgl_agenda] => 2014-06-01
[jam_agenda] => 13:27:30
[komisi] => Array
(
[0] => 1
[1] => 3
)
[fraksi] => Array
(
[0] => 1
[1] => 4
)
[badan] => Array
(
[0] => 1
[1] => 3
)
[anggota] => Array
(
[0] => 1
[1] => 4
)
[bagian] => Array
(
[0] => 2
[1] => 4
)
)
My question is how to insert into database, in controller? Thank's for help. I'll appreciate.
Since your structure is not well formed for insert_batch method. Your need to restructure it first. Consider this example:
$original_values = array(
'nama_agenda' => 'blalala',
'kilasan' => 'asdsadsadasd',
'tgl_agenda' => '2014-06-01',
'jam_agenda' => '13:27:30',
'komisi' => array(1, 3),
'fraksi' => array(1, 4),
'badan' => array(1, 3),
'anggota' => array(1, 4),
'bagian' => array(2, 4),
);
$new_values = array();
for($x = 0, $size = count($original_values['komisi']); $x < $size; $x++) {
foreach($original_values as $key => &$value) {
if(!is_array($value)) {
$new_values[$x][$key] = $value;
} else {
$new_values[$x][$key] = array_shift($value);
}
}
}
echo '<pre>';
print_r($new_values);
Should yield something like:
Array
(
[0] => Array
(
[nama_agenda] => blalala
[kilasan] => asdsadsadasd
[tgl_agenda] => 2014-06-01
[jam_agenda] => 13:27:30
[komisi] => 1
[fraksi] => 1
[badan] => 1
[anggota] => 1
[bagian] => 2
)
[1] => Array
(
[nama_agenda] => blalala
[kilasan] => asdsadsadasd
[tgl_agenda] => 2014-06-01
[jam_agenda] => 13:27:30
[komisi] => 3
[fraksi] => 4
[badan] => 3
[anggota] => 4
[bagian] => 4
)
)
Now you can use insert_batch() method.
$this->db->insert_batch('table_name', $new_values);
get all the data in array using $this->input->post() eg:
$bagian= $this->input->post('bagian');
and create a array()
$arr=array(
'db_table_col_1'=>$bagian,
'db_table_col_2'=>$post_data,
'db_table_col_2'=>$post_data
);
pass this array to model
$this->your_model_name->function_name($arr);
then in model create function
function_name($arg){
$this->db->insert('table_name',$arr);
}
if you want to insert multiple row then just use foreach
<?php
$arr1=array();
$arr= array(
'nama_agenda' => 'blalala',
'kilasan' => 'asdsadsadasd',
'tgl_agenda' => '2014-06-01',
'jam_agenda' => '13:27:30',
'komisi' => array
(
'0' => 1,
'1' => 3
),
'fraksi' => array
(
'0' => 1,
'1' => 4
),
'badan' => array
(
'0' => 1,
'1' => 3
),
'anggota' => array
(
'0' => 1,
'1' => 4
),
'bagian' => array
(
'0' => 2,
'1' => 4
)
);
foreach($arr as $row){
if(is_array($row)){
array_push($arr1,$row);
}
}
print_r($arr1);
and then pass this array to batch_insert
function_name($arr1){
$this->db->insert_batch('table_name',$arr1);
}
note arr1 syntax must be
$arr1 = array(
array(
'table_col1' => 'My title' ,
'table_col2' => 'My Name'
),
array(
'table_col1' => 'other title' ,
'table_col2' => 'other Name'
)
);
?>
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 ?
(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)
}
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.
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;
}
<?
$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' ) ) );
?>