Php 使数组元素复制它';s上一个元素';s值
我有一个数组,我想循环通过它,让它的元素使用PHP复制前面元素的值Php 使数组元素复制它';s上一个元素';s值,php,arrays,sorting,foreach,data-manipulation,Php,Arrays,Sorting,Foreach,Data Manipulation,我有一个数组,我想循环通过它,让它的元素使用PHP复制前面元素的值 array [ 17 => "x", 16 => "x", 15 => "x", 14 => "108.7000", 13 => "x", 12 => "x", 11 => "x", 10 => "x", 09 => "108.2635", 08 => "x", 07 => "
array [
17 => "x",
16 => "x",
15 => "x",
14 => "108.7000",
13 => "x",
12 => "x",
11 => "x",
10 => "x",
09 => "108.2635",
08 => "x",
07 => "x",
06 => "x",
05 => "x",
04 => "x",
03 => "x",
02 => "x",
01 => "x",
00 => "x",
23 => "x",
22 => "x",
21 => "x",
20 => "x",
19 => "x",
18 => "x"
]
例如,输出应为:
array [
17 => "108.7000",
16 => "108.7000",
15 => "108.7000",
14 => "108.7000",
13 => "108.2635",
12 => "108.2635",
11 => "108.2635",
10 => "108.2635",
09 => "108.2635",
08 => "108.2635",
07 => "108.2635",
06 => "108.2635",
05 => "108.2635",
04 => "108.2635",
03 => "108.2635",
02 => "108.2635",
01 => "108.2635",
00 => "108.2635",
23 => "108.2635",
22 => "108.2635",
21 => "108.2635",
20 => "108.2635",
19 => "108.2635",
18 => "108.2635"
]
如您所见,14以上的键变为108.7000,
字母“x”将永远存在,这就是我想要检查的方式。
我无法理解这个问题,也许我甚至没有用正确的措辞来描述这个问题
看看09键,它的值是108.2635,下面是x,这意味着它们都应该复制09的值,而09到04之间的值也是x,这意味着它们也应该是108.2635,键14有它自己的值,在它上面是x值,这意味着它们应该复制14的值如果不完全理解数组的结构,我只能提供以下方法:-
作为一个例子,考虑数组:-< /P>
[“x”、“x”、“x”、“34.34”、“x”、“x”、“20.02”、“x”]
- 在步骤1之后:-
lastNumber=“x”
- 步骤2和3:-
[“x”、“x”、“x”、“34.34”、“x”、“x”、“20.02”、“x”]
首先,lastNumber的
值将为“34.34”,因此以下所有“x”将替换为“34.34”:-
[“x”、“x”、“x”、“34.34”、“34.34”、“34.34”、“20.02”、“x”]
接下来,lastNumber的
值将是“20.02”,因此以下所有“x”都将替换为“20.02”:-
[“x”、“x”、“x”、“34.34”、“34.34”、“34.34”、“20.02”、“20.02”]
- 步骤4:-
$data = [ '17' => "x", '16' => "x", '15' => "x", '14' => "x", '13' => "108.7000", '12' => "x", '11' => "x", '10' => "x", '09' => "x", '08' => "x", '07' => "x", '06' => "x", '05' => "x", '04' => "x", '03' => "x", '02' => "x", '01' => "x", '00' => "x", '23' => "x", '22' => "x", '21' => "x", '20' => "x", '19' => "x", '18' => "108.2635"];
// Initially set reference as null. i.e. no reference value
$reference = null;
$group = [];
// Reverse the order so that we can traverse from bottom to up
$data = array_reverse($data, true);
// Traverse the array
foreach ($data as $key => $value)
{
/**
* If there is no reference by which we exchange the 'x' value
* then assign keys at an undefined group.
*/
if ($reference === null && $value === 'x')
{
$group['undefined'][] = $key;
}
elseif ($reference === null && $value !== 'x')
{
/**
* If reference not yet set but value is not 'x'
* then set the reference as the non-x value which we find just now.
* And change the 'undefined' group by newly found non-x value
*/
$reference = $value;
if (!empty($group['undefined']))
{
$group[$reference] = $group['undefined'];
unset($group['undefined']);
}
}
elseif ($reference !== null && $value === 'x')
{
/**
* If reference is set and value is 'x'
* then push into the key into reference group.
*/
$group[$reference][] = $key;
}
elseif ($reference !== null && $value !== 'x')
{
/**
* If reference is not null and value is also not 'x'
* then change the reference by newly found non-x value
* and make a new group and push the key into the group
*/
$reference = $value;
$group[$reference][] = $key;
}
}
/**
* Traverse the group and exchange the 'x' values
* from the group reference.
*/
foreach ($group as $value => $refs)
{
foreach ($refs as $key)
{
$data[$key] = $value;
}
}
/**
* Finally get the original order of the data array
*
*/
$data = array_reverse($data, true);
print_r($data);
因此,
x
指向最近的下一个值,或最近的previos?这08=>“x”
x值是如何108.2635
?@YosefTukachinsky不,x总是在那里,它可以在任何地方,基本上,x意味着这个元素应该复制一个不是xx的值,这个值被哪个条件替换了?为什么19以上和19以下的元素都有相同的值-108.2635?@SajeedAhamed它工作得很好,只是当最后一个键的值不是x时出现问题,那么它会给出错误,在这种情况下,假设键18有一个值,但它不是XT,那么我就不会得到任何错误。它正在工作。我在SajeebAhamed为正值,当我给最后一个元素一个值时,我得到了这个错误:未定义的索引:未定义的$data=['17'=>“x”、'16'=>“x”、'15'=>“x”、'14'=>“x”、'13'=>“108.7000”、'12'=>“x”、'11'=>“x”、'10'=>“x”、'09'=>“x”、'08'=>“x”、'07'=>“x”、'06'=>“x”、'05'>“x”、'03'>“x”、'01'=>“x”、'00'=>“x”、'23'=>“x”、'22'=>“x”、'21'=>“x”、'20'=>“x”、'19'=>“x”、'18'=>“108.2635”];