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' ) ) );
?>
Related
I'm sorry for not using the correct terminology. I'm very much a 'weekend warrior' when it comes to programming, but trying to get better.
I have two indexed arrays with associative arrays as the values. ArrayOne has a value for [uid_apps] that I want to use as a filter for ArrayTwo so I can create ArrayThree. The thrid array will only include array items from ArrayTwo that match the [uid_apps] value in ArrayOne.
I looked ar array_combine() and array_intersect(), but I didn't see a clear path to success. I also messed around with array_filter(), but wasn't able to make it work.
Below are samples of arrayOne, arrayTwo, and the desired arrayThree. Any help you can offer is greatly appreciated.
ArrayOne
[0] => Array
(
[uid_appMembership] => 3
[uid_apps] => 1
[uid_main] => 3
[privileges] => 555
)
[1] => Array
(
[uid_appMembership] => 4
[uid_apps] => 3
[uid_main] => 3
[privileges] => 555
)
ArrayTwo
[0] => Array
(
[uid_apps] => 1
[name_apps] => GHS Walk Through Evaluation
[site_apps] => ghs_001
[team_apps] => ghs_admin
[admin_uid] => 2
[dir_apps] => ghs_walk-through-evaluation
)
[1] => Array
(
[uid_apps] => 2
[name_apps] => CTE Work Based Learning Solution
[site_apps] => do_000
[team_apps] => do_cte
[admin_uid] => 3
[dir_apps] => do_cte-wbl
)
[2] => Array
(
[uid_apps] => 3
[name_apps] => GHS Parking Permit Solution
[site_apps] => ghs_001
[team_apps] => ghs_parking
[admin_uid] => 3
[dir_apps] => ghs_parking-permits
)
[3] => Array
(
[uid_apps] => 4
[name_apps] => GHS F-List
[site_apps] => ghs_001
[team_apps] => ghs_counseling
[admin_uid] => 3
[dir_apps] => ghs_flist
)
Desired ArrayThree I'd like to use arrayOne and arrayTwo to create this array.
[0] => Array
(
[uid_apps] => 1
[name_apps] => GHS Walk Through Evaluation
[site_apps] => ghs_001
[team_apps] => ghs_admin
[admin_uid] => 2
[dir_apps] => ghs_walk-through-evaluation
)
[1] => Array
(
[uid_apps] => 4
[name_apps] => GHS F-List
[site_apps] => ghs_001
[team_apps] => ghs_counseling
[admin_uid] => 3
[dir_apps] => ghs_flist
)
loop through array and check uid_apps value is available in other array or not if available than add it in third array.check using in array.
$arr1 = array(
array(
'uid_appMembership' => 3,
'uid_apps' => 1,
'uid_main' => 3,
'privileges' => 555
),
array(
'uid_appMembership' => 4,
'uid_apps' => 3,
'uid_main' => 3,
'privileges' => 555
)
);
$arr2 = array(
array(
'uid_apps' => 1,
'name_apps' => 'GHS Walk Through Evaluation',
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_admin',
'admin_uid' => 2,
'dir_apps' => 'ghs_walk-through-evaluation'
),array(
'uid_apps' => 2,
'name_apps' => 'CTE Work Based Learning Solution',
'site_apps' => 'do_000',
'team_apps' => 'do_cte',
'admin_uid' => 3,
'dir_apps' => 'do_cte-wbl'
),array(
'uid_apps' => 3,
'name_apps' => 'GHS Parking Permit Solution' ,
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_parking',
'admin_uid' => 3,
'dir_apps' => 'ghs_parking-permits'
),array(
'uid_apps' => 4,
'name_apps' => 'GHS F-List',
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_counseling',
'admin_uid' => 3,
'dir_apps' => 'ghs_flist'
)
);
$arr3 = array();
foreach ($arr2 as $key => $value) {
$res = chk_val($arr1,$value['uid_apps']);
if($res == true){
array_push($arr3,$arr2[$key]);
}
}
function chk_val($arr,$val){
foreach ($arr as $key => $value) {
if(in_array($val,$value)){
return true;
}else{
return false;
}
}
}
Working example : http://phpfiddle.org/main/code/sdri-fbpk
here is how you can get desired array
$dataa = array();
foreach ($names as $key => $name) {
foreach($ips as $key2=>$ip){
if($name['uid_apps'] == $ip['uid_apps']){
$dataa[] = $name;
}
}
}
print_r($dataa);
I have two multi-dimensional associative array ,
first we have
Array
(
[user_authentication] => Array
(
[api_user_id] => xxxxxxxxxxxxxxxxxxxxxxxx
[api_auth_token] => xxxxxxxxxxxxxxxxxxxxxx
)
[campaign_details] => Array
(
[campaign_name] => democampaign
[campaign_category] => appsGames
[campaign_sub_category] => Action
[campaign_type] => cpc
[campaign_start_date] => MM/DD/YYYY
[campaign_end_date] => MM/DD/YYYY
[campaign_start_time] => HH:mm
[campaign_end_time] => HH:mm
)
[campaign_budget_info] => Array
(
[campaign_daily_budget] => 0.2
[campaign_hourly_budget] => 0.3
[campaign_bid] => 0.1
[campaign_budget] => 1
)
[campaign_targetting_info] => Array
(
[campaign_os_type] => Apple
[country_code] => IN,AF,AG
[state_id] => Array
(
[IN] => 1,2,3
[AF] => 4,5,6
[AG] => 7,8,9
)
[carrier] => Array
(
[IN] => Tata,Aircel,RCOM,Vodafone,Airtel,Idea Cellular,Uninor,Dishnet,BSNL
[AF] =>
[AG] =>
)
[isp] =>
[device_targeting] => iphone,ipad
[conversion] =>
)
[campaign_creative_info] => Array
(
[campaign_domain] => abcd.com
[campaign_click_url] => http://url-to-redirect-users-to-after-they-click.com/
[campaign_banner_size] => URL640x1136
[campaign_banner_url] => http://imageurl.com/
[campaign_creative_type] => image
)
[campaign_black_list_white_list_info] => Array
(
[black_list_app_ids] => 5388dceb96c4b54a0844e4cb,5330b3864dab485e6219ff54
[black_list_device_ids] => aaaaaaaa-bbbb-cccc-0000-222222221111,12f93cf2-91ed-4f8f-aae7-a0520bebdd2r
[black_list_ip_addresses] => 123.123.12.123,10.100.100.100
[white_list_app_ids] => 5388dceb96c4b54a0844e4cb,5330b3864dab485e6219ff54
[white_list_device_ids] => aaaaaaaa-bbbb-cccc-0000-222222221111,12f93cf2-91ed-4f8f-aae7-a0520bebdd2r
[white_list_ip_addresses] => 123.123.12.123,10.100.100.100
)
)
and second one is which i have make to compare with
Array
(
[user_authentication] => Array
(
[api_user_id] => 1
[api_auth_token] => 1
)
[campaign_details] => Array
(
[campaign_name] => 1
[campaign_category] => 1
[campaign_sub_category] => 1
[campaign_type] => 1
[campaign_start_date] => 1
[campaign_end_date] => 1
[campaign_start_time] => 1
[campaign_end_time] => 1
)
[campaign_budget_info] => Array
(
[campaign_daily_budget] => 1
[campaign_hourly_budget] => 1
[campaign_bid] => 1
[campaign_budget] => 1
)
[campaign_targetting_info] => Array
(
[campaign_os_type] => 1
[country_code] => 1
[state_id] => Array
(
[IN] => 1
[AF] => 1
[AG] => 1
)
[carrier] => Array
(
[IN] => 1
[AF] => 1
[AG] => 1
)
[isp] => 1
[device_targeting] => 1
[conversion] => 1
)
[campaign_creative_info] => Array
(
[campaign_domain] => 1
[campaign_click_url] => 1
[campaign_banner_size] => 1
[campaign_banner_url] => 1
[campaign_creative_type] => 1
)
[campaign_black_list_white_list_info] => Array
(
[black_list_app_ids] => 1
[black_list_device_ids] => 1
[black_list_ip_addresses] => 1
[white_list_app_ids] => 1
[white_list_device_ids] => 1
[white_list_ip_addresses] => 1
)
)
we have to compare the array and find which key is missing in first array
i have tried this but not working
$comparemodel= array_diff_assoc($array1,$array2);
if($comparemodel==0){
echo "hello";
}
else{
$keys = array_keys($comparemodel);
for ($i = 0; $i < count($keys); $i++) {
$error_message[] = $keys[$i] . " is missing";
}
$model = array();
$errors = array("error_code" => 3042, "error_message" => $error_message);
$message = $error_message;
$status = 0;
$finalarray = array("modal" => $model, "errors" => $errors, "message" => $message, "status" => $status);
echo json_encode($finalarray);
}
its not working with this associative array but its working with simple array. what should i do for this.
thanks
try this code
<?php
$arr1=array("campaign_details" => array
(
"campaign_name" => "democampaign",
"campaign_category" => "appsGames",
"campaign_sub_category" => "Action",
"campaign_type" => "cpc",
"campaign_start_date" => "MM/DD/YYYY",
"campaign_end_date" => "MM/DD/YYYY",
"campaign_start_time" => "HH:mm",
"campaign_end_time" => "HH:mm"
),
"campaign_budget_info" => array
(
"campaign_daily_budget" => 0.2,
"campaign_hourly_budget" => 0.3,
"campaign_bid" => 0.1,
"campaign_budget" => 1,
),
"campaign_targetting_info" => array
(
"campaign_os_type" => "Apple",
"country_code" => "IN,AF,AG",
"state_id" => array
(
"IN" => "1,2,3",
"AF" => "4,5,6",
"AG" => "7,8,9"
),
"carrier" => Array
(
"IN" => "Tata,Aircel,RCOM,Vodafone,Airtel,Idea
Cellular,Uninor,Dishnet,BSNL",
"AF" => "",
"AG" => "",
),
"isp" => "",
"device_targeting" => "iphone,ipad",
"conversion" => "",
),
"campaign_creative_info" => array
(
"campaign_domain" => "abcd.com",
"campaign_click_url" => "http://url-to-redirect-users-to-after-
they-click.com/",
"campaign_banner_size" => "URL640x1136",
"campaign_banner_url" => "http://imageurl.com/",
"campaign_creative_type" => "image",
),
"campaign_black_list_white_list_info" => array
(
"black_list_app_ids" =>
"5388dceb96c4b54a0844e4cb,5330b3864dab485e6219ff54",
"black_list_device_ids" => "aaaaaaaa-bbbb-cccc-
0000-222222221111,12f93cf2-91ed-4f8f-aae7-a0520bebdd2r",
"black_list_ip_addresses" => "123.123.12.123,10.100.100.100",
"white_list_app_ids" =>
"5388dceb96c4b54a0844e4cb,5330b3864dab485e6219ff54",
"white_list_device_ids" => "aaaaaaaa-bbbb-cccc-
0000-222222221111,12f93cf2-91ed-4f8f-aae7-a0520bebdd2r",
"white_list_ip_addresses" => "123.123.12.123,10.100.100.100",
)
);
$arr2=array("campaign_details" =>array
(
"campaign_name" => 1,
"campaign_category" => 1,
"campaign_sub_category" => 1,
"campaign_type" => 1,
"campaign_start_date" => 1,
"campaign_end_date" => 1,
"campaign_start_time" => 1,
"campaign_end_time" => 1
),
"campaign_budget_info" => array
(
"campaign_daily_budget" => 1,
"campaign_hourly_budget" => 1,
"campaign_bid" => 1,
"campaign_budget" => 1,
),
"campaign_targetting_info" => array
(
"campaign_os_type" => 1,
"country_code" => 1,
"state_id" => array
(
"IN" => 1,
"AF" => 1,
"AG" => 1,
),
"carrier" => array
(
"IN" => 1,
"AF" => 1,
"AG" => 1,
),
"isp" => 1,
"device_targeting" => 1,
"conversion" => 1,
),
"campaign_creative_info" =>array
(
"campaign_domain" => 1,
"campaign_click_url" => 1,
"campaign_banner_size" => 1,
"campaign_banner_url" => 1,
"campaign_creative_type" => 1,
),
"campaign_black_list_white_list_info" => array
(
"black_list_app_ids" => 1,
"black_list_device_ids" => 1,
"black_list_ip_addresses" => 1,
"white_list_app_ids" => 1,
"white_list_device_ids" => 1,
"white_list_ip_addresses" => 1,
)
);
function array_keys_multi(array $array)
{
$keys = array();
foreach ($array as $key => $value) {
$keys[] = $key;
if (is_array($array[$key])) {
$keys = array_merge($keys, array_keys_multi($array[$key]));
}
}
return $keys;
}
$resArr=array();
$a=array_keys_multi($arr1);
$b=array_keys_multi($arr2);
$c=array_diff($a,$b);
if(count($c) > 0){
echo "There is differnce<br/>";
echo "<pre>";
print_r($c);
}else
echo "There is no differnce<br/>";
?>
I have search for a solution for this but couldn't find anything giving me a "straight" multidimensional array back. Flatten is probably not the solution as long as i want to preserve the original sub structure?
In additional i want to summarize qty when the key is repeating.
This is my original array:
Array
(
[60002] => Array
(
[50001] => Array
(
[50002] => Array
(
[10001] => Array
(
[flag] => B
[qty] => 1
)
[10002] => Array
(
[flag] => B
[qty] => 1
)
[10003] => Array
(
[flag] => B
[qty] => 2
)
[flag] => M
[qty] => 1
)
[flag] => M
[qty] => 1
)
[flag] => G
[qty] => 1
)
[10001] => Array
(
[flag] => B
[qty] => 1
)
)
What i basically want is to create a new array looking like this:
Array
(
[10001] => Array
(
[flag] => B
[qty] => 2
)
[10002] => Array
(
[flag] => B
[qty] => 1
)
[10003] => Array
(
[flag] => B
[qty] => 2
)
[50001] => Array
(
[flag] => M
[qty] => 1
)
[50002] => Array
(
[flag] => M
[qty] => 1
)
[60002] => Array
(
[flag] => G
[qty] => 1
)
)
This is tested.
The key is intval().
$value['qty'] += intval($newArray[$key]['qty']);
If the [$key]['qty'] does not exist the intval() will return a zero. This is much faster than using an if else to check if a [$key]['qty'] already exists.
The only possible problem I could anticipate is if the Flag value is different when the key value is the same:
[10001] => Array(
[flag] => M
[qty] => 1
),
[10001] => Array(
[flag] => B
[qty] => 1
)
When this is an issue I resolve the priority with a logic table in an array.
$priority['M']['B'] = 'M'
$priority['B']['M'] = 'M'
$priority['']['M'] = 'M'
$priority['M'][''] = 'M'
$priority['B'][''] = 'B'
$priority['B'][''] = 'B'
settype($newArray[$key]['flag'],'string');
[$newArray[$key]['flag'] = $priority[$value['flag']][$newArray[$key]['flag']]
Data:
$array = array('60002' => Array('50001' => Array('50002' => Array('10001' => Array('flag' => 'B','qty' => 1),'10002' => Array('flag' => 'B','qty' => 1),'10003' => Array('flag' => 'B','qty' => 2),'flag' => 'M','qty' => 1),'flag' => 'M','qty' => 1),'flag' => 'G','qty' => 1),'10001' => Array('flag' => 'B','qty' => 1));
PHP
$newArray = array();
getValues($data);
function getValues($array){
global $newArray;
foreach ($array as $key => $value){
if(is_numeric($value['qty'])) {
$value['qty'] += intval($newArray[$key]['qty']);
$newArray[$key] = array('flag'=>$value['flag'],'qty'=>$value['qty']);
}
if (gettype($value) != 'array'){return;}
getValues($value);
}
}
ksort($newArray);
var_export($newArray);
Result:
array (
10001 =>
array (
'flag' => 'B',
'qty' => 2,
),
10002 =>
array (
'flag' => 'B',
'qty' => 1,
),
10003 =>
array (
'flag' => 'B',
'qty' => 2,
),
50001 =>
array (
'flag' => 'M',
'qty' => 1,
),
50002 =>
array (
'flag' => 'M',
'qty' => 1,
),
60002 =>
array (
'flag' => 'G',
'qty' => 1,
),
)
This seemed to work:
function extractArray(array $source, array &$destination, $originalIndex)
{
foreach($source as $index => $value)
{
if(is_array($value))
extractArray($value, $destination, $index);
else
$destination[$originalIndex][$index] = $value;
}
}
$test = array(
60002 => array
(
50001 => array
(
50002 => array
(
10001 => array
(
'flag' => 'B',
'qty' => 1
),
10002 => array
(
'flag' => 'B',
'qty' => 1
),
10003 => array
(
'flag' => 'B',
'qty' => 2
),
'flag' => 'M',
'qty' => 1
),
'flag' => 'M',
'qty' => 1
),
'flag' => 'G',
'qty' => 1
),
10001 => array
(
'flag' => 'B',
'qty' => 1
)
);
$new = array();
foreach($test as $index => $value)
extractArray($value, $new, $index);
var_dump($new);
die();
You can use a recursive approach to iterate over all levels of the array. For each item you check that it is an array and if it has any of the keys you want checked, add the array consisting of the found attributes.
function flattenArray($array, $keysToCheck) {
$result = array();
foreach($array as $item) {
// check if the current array item is a candidate to
// be added to the flattened array
if(is_array($item)) {
$foundAttributes = array();
foreach($item as $key=>$value) {
if(in_array($key, $keysToCheck) {
$foundAttributes[$key] = $value;
}
}
// we found at least one matching attribute
if(count($foundAttributes)) {
array_push($result, $foundAttributes);
}
// recursively go to the next level and merge the results from there
$result = array_merge($result, flattenArray($item, $keysToCheck);
}
}
return $result;
}
// usage example
$flattenedArray = flattenArray($originalArray, array('flag', 'qty'));
The above solution allows you to customise it for other type of objects, by passing different $keysToCheck arguments to the function.
If you also need the flattened array sorted, you can use usort() to achieve this.
Please pardon any syntax errors, I don't have a PHP interpreter at hand.
I have the following result from a MySQL query with two joins.
Array (
[0] => Array ( [place_id] => 1 [place] => Berlin [lat] => 52.519 [lon] => 13.406 [id] => 1 [pname] => Firstschool [typ] => 0 [s_id] => 32 [fac] => history)
[1] => Array ( [place_id] => 1 [place] => Berlin [lat] => 52.519 [lon] => 13.406 [id] => 1 [pname] => Secondschool [typ] => 0 [s_id] => 33 [fac] => math)
[2] => Array ( [place_id] => 1 [place] => Berlin [lat] => 52.519 [lon] => 13.406 [id] => 1 [pname] => Secondschool [typ] => 0 [s_id] => 33 [fac] => english)
)
The data is redundant at some points, I need it this way:
Array (
[Berlin] => Array ( [lat] => 52.519
[lon] => 13.406
[schools] => Array([0]=> Firstschool [1]=>Secondschool)
)
[OtherCity] => Array ( ... )
)
First, is this okay or exists a better solution? =)
Second.. how to split it for the needed result.
I tried it with something like the following code snippet, but it doesn't work as wished.
foreach($viewmodel as $item) {
$data[$item['place']][] = $item['pname'];
}
The result is:
Array ( [Berlin] => Array ( [0] => Firstschool [1] => Firstschool [2] => Firstschool ))
NOT so useful. ;)
I hope its understandable what I need. Maybe someone has an nice idea how to solve this problem.
Thanks for your time.
I think you are on a right path, just need to fill in a little more detail:
$cities = Array (
Array ( 'place_id' => 1, 'place' => 'Berlin', 'lat' => 52.519, 'lon' => 13.406, 'id' => 1, 'pname' => 'Firstschool', 'typ' => 0, 's_id' => 32, 'fac' => 'history'),
Array ( 'place_id' => 1, 'place' => 'Berlin', 'lat' => 52.519, 'lon' => 13.406, 'id' => 1, 'pname' => 'Secondschool', 'typ' => 0, 's_id' => 33, 'fac' => 'math'),
Array ( 'place_id' => 1, 'place' => 'Berlin', 'lat' => 52.519, 'lon' => 13.406, 'id' => 1, 'pname' => 'Secondschool', 'typ' => 0, 's_id' => 33, 'fac' => 'english'),
);
// gather the transformed array in a new array
$out = array();
foreach ($cities as $city) {
// the first time we see the place
if (!isset($out[$city['place']])) {
// copy over what you want to keep
$out[$city['place']] = array(
'lat' => $city['lat'],
'lon' => $city['lon'],
'schools' => array($city['pname']),
);
} // only add $city['pname'] if we don't have it already
elseif (!in_array($city['pname'], $out[$city['place']]['schools'])) {
// we already seen this place, just add to the schools
$out[$city['place']]['schools'][] = $city['pname'];
}
}
For the gather faculties too question, use the school names as keys to arrays in the 'schools' key of the top level arrays, populate them like this: (still skipping duplicates):
foreach ($a as $city) {
if (!isset($out[$city['place']])) {
$out[$city['place']] = array(
'lat' => $city['lat'],
'lon' => $city['lon'],
'schools' => array($city['pname'] => array($city['fac'])),
);
} else {
// for convenience and readability, introducing some variables
$schools = &$out[$city['place']]['schools'];
$pname = $city['pname'];
$fac = $city['fac'];
// if we didn't see this school yet, add it with it's faculty
if (!isset($schools[$pname])) {
$schools[$pname] = array($fac);
} // if we did see this school before but the faculty is new, add it under the school's key
else if (!in_array($fac, $schools[$pname])) {
$schools[$pname][] = $fac;
}
}
}
you are right, you have to iterate through the array some way or another. From the array I have seen, assuming all latitudes and longitudes are the same for all schools, overwriting will not hurt, otherwise, additional logic is needed
foreach($viewmodel as $item) {
$data[$item['place']['lat']=$item['lat'];
$data[$item['place']['long']=$item['lon'];
$data[$item['place']['schools'][]=$item['pname'];
}
You could map the array using a lambda function if you're using php 5.3+
$output = array();
$sort_schools = function($value, $key)
{
if ( ! is_array($output[$value['place'])
{
$output[$value['place'] = array();
}
if ( ! isset($output[$value['place']['lat'] && ! isset($output[$value['place']]['lon'])
{
$output[$value['place']]['lat'] = $value['lat'];
$output[$value['place']]['lon'] = $value['lon'];
}
$output[$value['place']]['schools'][] = $value['pname'];
};
array_map($sort_schools, $viewmodel);
Alternatively you could use a similar structure in the lambda function within a foreach loop or an anonymous function.
The following should yield the described expected result
$arr = array(
array( 'place_id' => 1, 'place' => 'Berlin', 'lat' => 52.519, 'lon' => 13.406, 'id' => 1, 'pname' => 'Firstschool', 'typ' => 0, 's_id' => 32, 'fac' => 'history'),
array( 'place_id' => 1, 'place' => 'Berlin', 'lat' => 52.519, 'lon' => 13.406, 'id' => 1, 'pname' => 'Secondschool', 'typ' => 0, 's_id' => 32, 'fac' => 'history'),
array( 'place_id' => 1, 'place' => 'Berlin', 'lat' => 52.519, 'lon' => 13.406, 'id' => 1, 'pname' => 'Secondschool', 'typ' => 0, 's_id' => 32, 'fac' => 'history')
);
$result = array();
foreach($arr as $item) {
if (array_key_exists($item['place'], $result)) {
if (!in_array($item['pname'], $result[$item['place']]['schools'])) {
array_push($result[$item['place']]['schools'], $item['pname']);
}
} else {
$result[$item['place']]['lat'] = $item['lat'];
$result[$item['place']]['lon'] = $item['lon'];
$result[$item['place']]['schools'][] = $item['pname'];
}
}
print_r($result);
Which should output
Array (
[Berlin] => Array
(
[lat] => 52.519
[lon] => 13.406
[schools] => Array
(
[0] => Firstschool
[1] => Secondschool
)
)
)
I have two arrays as follows:
Array I:
Array
(
[0] => Array
(
[did] => 1
[dname] => AJAy
[dsp] => 3
[dqu] => abc
[isactive] => Y
)
[1] => Array
(
[did] => 2
[dname] => Vijay
[dsp] => 4
[dqu] => abc
[isactive] => Y
)
)
Array II:
Array
(
[0] => Array
(
[recno] => 1
[dname] => AJAy
[dsp] =>
[did]=>
[dqu] =>
[isactive] => Y
)
[1] => Array
(
[recno] => 2
[dname] => Vijay
[dsp] =>
[did]=>
[dqu] =>
[isactive] => Y
)
)
I want to update values of did, dsp, dqu from array I to array II based on match value of dname , I tried with array merge its not work out for my situation, please help me
How about:
$arr1 = Array(
Array(
'did' => 1,
'dname' => 'AJAy',
'dsp' => 3,
'dqu' => 'abc',
'isactive' => 'Y',
),
Array(
'did' => 2,
'dname' => 'Vijay',
'dsp' => 4,
'dqu' => 'abc',
'isactive' => 'Y',
),
);
$arr2 = Array(
Array(
'recno' => 2,
'dname' => 'Vijay',
'dsp' => '',
'did' => '',
'dqu' => '',
'isactive' => 'Y',
),
Array(
'recno' => 1,
'dname' => 'AJAy',
'dsp' => '',
'did' => '',
'dqu' => '',
'isactive' => 'Y',
),
);
for($i1=0; $i1<count($arr1); $i1++) {
for ($i2=0; $i2<count($arr2); $i2++) {
if ($arr1[$i1]['dname'] == $arr2[$i2]['dname']) {
$arr2[$i2]['did'] = $arr1[$i1]['did'];
$arr2[$i2]['dsp'] = $arr1[$i1]['dsp'];
$arr2[$i2]['dqu'] = $arr1[$i1]['dqu'];
}
}
}
print_r($arr2);
output:
Array
(
[0] => Array
(
[recno] => 2
[dname] => Vijay
[dsp] => 4
[did] => 2
[dqu] => abc
[isactive] => Y
)
[1] => Array
(
[recno] => 1
[dname] => AJAy
[dsp] => 3
[did] => 1
[dqu] => abc
[isactive] => Y
)
)
something like this?
<?php
$a = array
(
0 => array
(
1 => 'bat'
),
1 => array
(
10 => 'hamar'
)
);
$b = array
(
0 => array
(
2 => 'bi'
),
1 => array
(
11 => 'hamaike'
)
);
$length = count($a);
$tmp = array();
for($i=0;$i<$length;$i++)
{
$tmp[$i] = array_merge($a[$i], $b[$i]);
}
print_r($tmp);
?>
edit: it's better array_merge_recursive from comments :)
Dude you can try with array_combine() ...
it will Creates an array by using one array for keys and another for its values..
but you have to give condition.for matching values..