开发者

Autofill array with empty data to match another array size

I have 2 sets of arrays:

$dates1 = array('9/12',开发者_如何转开发'9/13','9/14','9/15','9/16','9/17');
$data1 = array('5','3','7','7','22','18');
// for this dataset, the value on 9/12 is 5

$dates2 = array('9/14','9/15');
$data2 = array('12','1');

As you can see the 2nd dataset has fewer dates, so I need to "autofill" the reset of the array to match the largest dataset.

$dates2 = array('9/12','9/13','9/14','9/15','9/16','9/17');
$data2 = array('','','12','1','','');

There will be more than 2 datasets, so I would have to find the largest dataset, and run a function for each smaller dataset to properly format it.

The function I'd create is the problem for me. Not even sure where to start at this point. Also, I can format the date and data arrays differently (multidimensional arrays?) if for some reason that is better.


You can do this in a pretty straightforward manner using some array functions. Try something like this:

//make an empty array matching your maximum-sized data set
$empty = array_fill_keys($dates1,'');

//for each array you wish to pad, do this:
//make key/value array
$new = array_combine($dates2,$data2);
//merge, overwriting empty keys with data values
$new = array_merge($empty,$new);
//if you want just the data values again
$data2 = array_values($new);

print_r($data2);

It would be pretty easy to turn that into a function or put it into a for loop to operate on your array sets. Turning them into associative arrays of key/value pairs would make them easier to work with too I would think.


If datas are related will be painful to scatter them on several array. The best solution would be model an object with obvious property names and use it with related accessor.

From your question I haven't a lot of hint of what data are and then I have to guess a bit:

I pretend you need to keep a daily log on access on a website with downloads. Instead of using dates/data1/data2 array I would model a data structure similar to this:

$log = array( 
    array('date'=>'2011-09-12','accessCount'=>7,'downloadCount'=>3),
    array('date'=>'2011-09-13','accessCount'=>9), /* better downloadsCount=>0 though */
    array('date'=>'2011-09-15','accessCount'=>7,'downloadCount'=>3)
    ...
)

Using this data structure I would model a dayCollection class with methods add,remove,get,set, search with all methods returning a day instance (yes, the remove too) and according signature. The day Class would have the standard getter/setter for every property (you can resolve to magic methods).

Depending on the amount of data you have to manipulate you can opt to maintain into the collection just the object data (serialize on store/unserialize on retrieve) or the whole object.

It is difficult to show you some code as the question is lacking of details on your data model.

If you still want to pad your array than this code would be a good start:

$temp = array($dates, $data1, $data2);
$max = max(array_map('count',$temp));
$result = array_map( function($x) use($max) {
    return array_pad($x,$max,0);
}, $temp);

in $result you have your padded arrays. if you want to substitute your arrays do a simple

list($dates, $data1, $data2) = array_map(....


You should use hashmaps instead of arrays to associate each date to a data.

Then, find the largest one, cycle through its keys with a foreach, and test the existence of the same key in the small one.

If it doesn't exist, create it with an empty value.


EDIT with code (for completeness, other answers seem definitely better):

$dates_data1 = array('9/12'=>'5', '9/13'=>'3', '9/14'=>'7' /* continued */);
$dates_data2 = array('9/14'=>'12', '9/15'=>'1');

#cycle through each key (date) of the longest array
foreach($dates_data1 as $key => $value){
  #check if the key exists in the smallest and add '' value if it does not
  if(!isset( $date_data2[$key] )){ $date_data2[$key]=''; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜