I have an array that occasionally has random values empty. Rather than delete, I need to replace the empty value with the closest (proximity) non-empty array value.
How can I do this?
I have an array that occasionally has random values empty. Rather than delete, I need to replace the empty value with the closest (proximity) non-empty array value.
How can I do this?
{Pseudocode time)
lastvalue = first() - 1;
foreach the array;
if (currentvalue != lastvalue + 1) {
for(each value between current and last) {
set value = ternaryif(difftocurrent > difftolast) then last else current
}
}
store new last
endforeach
(last and lastvalue are different variables - the former holding the value of the array element, and the latter holding the value of the array key.)
How about a target array?
$before = array(12,34,null,1,2,3,22,null,23,24,25,null,25,17,29,null,null,1);
Is that a good enough test?
What should the $after array look like after the treatment?
Yeah should be good enough to test. It doesn’t matter whether it takes a value from before or after, so long as it’s the next along in the array which is not null!
eg. Looking forwards:
$after = array(12,34,1,1,2,3,22,23,23,24,25,25,25,17,29,1,1,1);
EDIT: actually a better test would be to have some nulls at the end, so the function designed goes backwards if necessary?
Maybe we need to know the structure of the array, yes… I assumed it looked something like
$before = array( 1=> 5,3 => 7,4 => 5, 7 => 2 ....)
making the after look like
array(1 => 5,2 => 7,3 => 7,4 => 5, 5 => 5, 6 => 2, 7 => 2 ...);
More like: $before = array( 1=> 5,2=>,3 => 7,4 => 5,5=>,6=>,7 => 2 …)
mkay… given Cups’ input then the pseudocode becomes:
$last = -1;
foreach $before
if current is not null
if last is not current - 1 {
for(from last+1 to current-1)
value = ternaryif(diffcurrent > difflast) then lastvalue else currentvalue
endfor
}
last = current
}
endforeach
there is a big difference between what Cups described and what I did; numerical arrays are indexed in PHP - so if you say
$array[0] = 1;
$array[2] = 2;
$array[1] is not “null” - it simply doesnt exist.
$array[0] = 1;
$array[1] = null;
$array[2] = 2;
$array[1] is null - but it does exist.
Big difference in how foreach will work - [FPHP]foreach[/FPHP] walks over each EXISTING element of the array, in order of it’s creation, which may not necessarily be numerical order. (S’why [FPHP]sort[/FPHP] exists
Does this work if the first few array values are null?
Hm, yes I see what you mean. The array I’m dealing with is:
array[timeset]=value
So missing values are eg array[timeset]=null
Sorry for the confusion!
So you’re pulling them out of… probably a database, and some of the records will have null in the field for value? And you dont want to add other timesets to the array, you just want to fill in the null ones with the closest value…
timeset is of what type?
yep: 00:00:06
Getting uglier in my head. This is completely untested and there’s probably a better way.
sort before
$keys = array_keys($before)
for from 0 to count(before) - 1 as i
if $before[$keys[i]] is null {
j = i + 1
while($before[$keys[j]] is null and j < count($before) -1) {
j++
}
if i > 0 and j < count(before)-1 {
$start = $keys[i]
$end = $keys[j]
for from i to j as k
$before[$keys[k]] = ternaryif(difftoj > difftoi) then $before[$keys[i]] else $before[$keys[j]]
endfor
i = j
} elseif i == 0 {
for from i to j as k
$before[$keys[k]] = $before[$keys[j]]
endfor
i = j
} else {
for from i to j as k
$before[$keys[k]] = $before[$keys[i-1]]
endfor
i = j
}
endfor
Thanks, I will try it out!