php


Fill missing date and prices (such as weekend) in array


I have an array with "gaps" like this:
$arr = array (
"bananas" => array (
"2010-01-01" => "10.1",
"2010-01-02" => "11.4",
"2010-01-05" => "13.3", //missing 03, 04
"2010-01-06" => "09.2",
"2010-01-07" => "11.1" ),
"grapes" => array (
"2011-01-05" => "20.1",
"2011-01-06" => "21.4",
"2011-01-07" => "23.3",
"2011-01-08" => "29.2",
"2011-01-10" => "21.1", //missing 09
"2011-01-11" => "21.0" ),
"coconuts" => array (
"2012-01-28" => "50.1",
"2012-01-29" => "51.4",
"2012-02-02" => "53.3", //missing 30, 31, 01
"2012-02-03" => "51.1" )
);
I don't know how many products and "date=>price" are there in an array.
How I can fill missing date(s) with last price before gap?
Important: dates can be in different months (look at "coconuts").
I want the result to be like this:
$arr = array (
"bananas" => array (
"2010-01-01" => "10.1",
"2010-01-02" => "11.4", //was last price before gap
"2010-01-03" => "11.4", //filling missing
"2010-01-04" => "11.4", //filling missing
"2010-01-05" => "13.3",
"2010-01-06" => "09.2",
"2010-01-07" => "11.1" ),
"grapes" => array (
"2011-01-05" => "20.1",
"2011-01-06" => "21.4",
"2011-01-07" => "23.3",
"2011-01-08" => "29.2", //was last price before gap
"2011-01-09" => "29.2", //filling missing
"2011-01-10" => "21.1",
"2011-01-11" => "21.0" ),
"coconuts" => array (
"2012-01-28" => "50.1",
"2012-01-29" => "51.4", //was last price before gap
"2012-01-30" => "51.4", //filling missing
"2012-01-31" => "51.4", //filling missing
"2012-02-01" => "51.4", //filling missing
"2012-02-02" => "53.3",
"2012-02-03" => "51.1" )
);
Thank you for your time.
<?php
$arr = array (
"bananas" => array (
"2010-01-01" => "10.1",
"2010-01-02" => "11.4",
"2010-01-05" => "13.3", //missing 03, 04
"2010-01-06" => "09.2",
"2010-01-07" => "11.1" ),
"grapes" => array (
"2011-01-05" => "20.1",
"2011-01-06" => "21.4",
"2011-01-07" => "23.3",
"2011-01-08" => "29.2",
"2011-01-10" => "21.1", //missing 09
"2011-01-11" => "21.0" ),
"coconuts" => array (
"2012-01-28" => "50.1",
"2012-01-29" => "51.4",
"2012-02-02" => "53.3", //missing 30, 31, 01
"2012-02-03" => "51.1" )
);
$result=array();
while(list($name,$data)=each($arr))
{
$result[$name]=array();
if(!empty($data))
{
$last_price=reset($data);
$cur_key=key($data);
$curdate=new DateTime($cur_key);
while(list($k, $price)=each($data))
{
$dt=new DateTime($k);
while($curdate<=$dt)
{
$cd=$curdate->format('Y-m-d');
if($curdate!=$dt) $result[$name][$cd]=$last_price;
else $result[$name][$cd]=$price;
$curdate->modify('+1 day');
}
$last_price=$price;
}
}
}
print_r($result);
?>
foreach ($arr as $fruit => $data)
{
$previousprice = 0;
$previoustime = false;
foreach ($data as $day => $price)
{
$time = strtotime($day);
if ($previoustime && (($time - $previoustime) > 86400))
$data[date('Y-m-d', ($previoustime + 86400))] = $previousprice;
$previousprice = $price;
$previoustime = $time;
}
ksort($data);
}

Related Links

Once an order is placed, how to fetch order details automatically in Magento?
control infinite-loops in php
htaccess redirect non-www to www the full path
how to insert dynamic array value in database using php
Symfony2 use of class
Extract SQL Query with JSON - Codeigniter
Client side download of image created by the user
Cannot connect Codeigniter to Sql Server 2016
Mysql join tables query in CakePhp
How can I pass configure options to phpenv install
unexpected token < JSON at position 0
Postman: How to execute Paypal CURL Request in Postman?
Wordpress/Woocommerce only show category to a single role
grouping in codeigniter sql
How To Use cURL without http://www Prefix
cakephp 3 and logout confusion

Categories

HOME
firebase
scroll
transactions
braintree
autocomplete
malware
web2py
openflow
phpmqtt
msmq
migrate
lapack
locationmanager
dma
bnf
nest-api
pygobject
string-formatting
android-cardview
points
basex
sendmessage
tightvnc
netbios
ms-media-foundation
xerces-c
nppexec
redgate
pdfnet
cargo
telephonymanager
aurelia-http-client
windows-store
swfupload
game-center
python-jira
grails-spring-security
jacoco-maven-plugin
data-uri
web-inspector
apache-toree
oracle-bmcs
cordova-ios
spring.net
punctuation
algebra
asp.net-web-api-routing
gnome-terminal
clrs
1010
dynamics-crm-4
zend-server
hmmlearn
ternary-operator
tsqlt
coovachilli
static-methods
gnucash
pack
textpattern
chicagoboss
roxygen2
android-handler
artisan
textscan
innerhtml
mgwt
tidyr
google-hangouts
uno
comctl32
dc
device-admin
inputbox
windows-rt
visual-studio-6
angular-ui-select
mfmessagecomposeview
miglayout
map
iosched
principalcontext
jquery-transit
notin
web-frameworks
oscilloscope
tridion-2011
xalan
hardcode
net-use
simile
ognl
turbine
explicit
reliability
3270
processors
data-retrieval
external-sorting
main-method
rakudo
objectinstantiation
activex-exe

Resources

Encrypt Message