PHP数组,通过将值数字减1在同一数组中查找来设置值


PHP array to set the value by finding in the same array by decresing the value digit by 1

我有一个多维数组。如下所示:

Array (
    [0] => Array (
        [0] => Array (
            [prefix] => 93
            [rate] => 0.172962000
            [destination] => Afghanistan
        )
    )
    [1] => Array (
        [0] => Array (
            [prefix] => 9320
            [rate] => 0
            [destination] => Afghanistan (ON-NET)
        )
    )
    [2] => Array (
        [0] => Array (
            [prefix] => 9350
            [rate] => 0
            [destination] => Afghanistan (ON-NET)
        )
    )
    [3] => Array (
        [0] => Array (
            [prefix] => 935
            [rate] => 0.451478000
            [destination] => Afghanistan (ON-NET)
        )
    )
    [4] => Array (
        [0] => Array (
            [prefix] => 9360
            [rate] => 0
            [destination] => Afghanistan (ON-NET)
        )
    )
);

我需要在具有rate = 0的数组中设置rate的值;

如何设置值:

  • 我需要从具有rate = 0的数组中选取前缀值;并且需要将前缀数字减少1(例如:prefix[9320]速率为0;现在需要减少1个前缀为932)。

  • 并在同一数组中搜索该值932。如果该值存在于阵列中并且具有速率,则需要将该速率值设置为prefix[9320]

  • 如果不需要再次像上面一样将前缀数字减少1,那么现在前缀是93。

  • 再次需要在数组中搜索93前缀速率值。如果速率存在,则需要将该速率设置为prefix[9320]

我试过这样:

foreach($resdata as $row) {
    if($row[0]['rate'] == 0) {
        $src = $row[0]['prefix']/10;
        foreach($resdata as $row2) {
            if(($src == $row2[0]['prefix']) && ($row2[0]['rate'] != 0)) {
                $row[0]['rate'] = $row2[0]['rate'];
            }
        }
    } 
}

我想要的最终数组如下

Array (
    [0] => Array (
        [0] => Array (
            [prefix] => 93
            [rate] => 0.172962000
            [destination] => Afghanistan
        )
    )
    [1] => Array (
        [0] => Array (
            [prefix] => 9320
            [rate] => 0.172962000
            [destination] => Afghanistan (ON-NET)
        )
    )
    [2] => Array (
        [0] => Array (
            [prefix] => 9350
            [rate] => 0.451478000
            [destination] => Afghanistan (ON-NET)
        )
    )
    [3] => Array (
        [0] => Array (
            [prefix] => 935
            [rate] => 0.451478000
            [destination] => Afghanistan (ON-NET)
        )
    )
    [4] => Array (
        [0] => Array (
            [prefix] => 9360
            [rate] => 0.172962000
            [destination] => Afghanistan (ON-NET)
        )
    )
);

也许您可以在前缀数字减少1时使用while循环,然后循环$resdata数组,看看是否有匹配的"前缀"来获得"速率"。

$resdata = array(
    array(array("prefix" => 93, "rate" => "0.172962000", "destination" => "Afghanistan")),
    array(array("prefix" => 9320, "rate" => "0", "destination" => "Afghanistan (ON-NET)")),
    array(array("prefix" => 9350, "rate" => "0", "destination" => "Afghanistan (ON-NET)")),
    array(array("prefix" => 935, "rate" => "0.451478000", "destination" => "Afghanistan (ON-NET)")),
    array(array("prefix" => 9360, "rate" => "0", "destination" => "Afghanistan (ON-NET)"))
);
function getRate($items, $prefix)
{
    while ($prefix = intval($prefix / 10)) {
        foreach ($items as $key => $value) {
            if ($value[0]["prefix"] === $prefix) {
                return $value[0]["rate"];
            }
        }
    }
    return null;
}
foreach ($resdata as $key => $row) {
    if ($row[0]['rate'] == 0) {
        $rate = getRate($resdata, $row[0]['prefix']);
        if ($rate) {
            $resdata[$key][0]['rate'] = $rate;
        }
    }
}

演示