用相邻的非默认值替换平面数组中的占位符元素

问题描述 投票:0回答:3

我有一个数组,我想循环遍历它,并使其元素复制前一个元素的值(如果它等于“x”)。

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
值。

php arrays replace foreach data-manipulation
3个回答
1
投票

试试这个-

$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);

1
投票

不完全了解你的数组结构,我只能提供给你方法:-

  1. 声明一个变量lastNumber,初始值为“x”。
  2. 循环遍历数组,当找到实际数字(!=“x”)时,将其存储在lastNumber中。
  3. 用最后一个数字替换所有后续的非数字(“x”)。
  4. 最后,反转阵列并再次执行步骤 1、2 和 3。

我知道这不是最优化的解决方案,但如果您了解发生了什么,您可以自己优化它。

作为示例,考虑一个数组:-

["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's
值将为“34.34”,因此以下所有“x”都将替换为“34.34”:-

["x", "x", "x", "34.34", "34.34", "34.34", "20.02", "x"]

接下来,

lastNumber's
值将为“20.02”,因此以下所有“x”都将替换为“20.02”:-

["x", "x", "x", "34.34", "34.34", "34.34", "20.02", "20.02"]

  • 第 4 步:-

现在,要将最初的“x”替换为“34.34”,请以相反的顺序对数组执行上述步骤。

如有任何问题,请随时提问。


0
投票

仅遍历数组一次。 在每个

x
值处,声明一个引用,以便在遇到非
x
值时进行修改。 仅当遇到非
x
2 次或更多次时,才有条件地取消设置引用。 这将确保以
x
值开头的数组也将被替换。 演示

foreach ($array as $k => $v) {
    if ($v === 'x') {
        $array[$k] =& $ref;
    } else {
        if (isset($ref)) {
            unset($ref);
        }
        $ref = $v;
    }
}
var_export($array);

输出:

array (
  17 => '108.7000',
  16 => '108.7000',
  15 => '108.7000',
  14 => '108.7000',
  13 => '108.7000',
  12 => '108.7000',
  11 => '108.7000',
  10 => '108.7000',
  '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',
)
© www.soinside.com 2019 - 2024. All rights reserved.