0

我已经在我的一个项目上工作了几天,基本上我在这个项目中要做的是比较用户通常在 excel 中执行的 csv 文件,每天晚上在 php 中自动执行。我从 CSV 的介绍数组中获得了信息,但是我无法将它们组合起来以获得每本书的正确信息,因此下面的示例和问题。

我有来自 csv 文件的以下数组(array1),在巫婆中 [5] 键将始终为 0 :

Array
(
[0] => Array
(
    [0] => book1
    [1] => description1
    [2] => category1
    [3] => code1
    [4] => editor1
    [5] => 0
    [6] => eur
    [7] => out of stoc
)

[1] => Array
(
    [0] => book2
    [1] => description2
    [2] => category2
    [3] => code2
    [4] => editor2
    [5] => 0
    [6] => curr2
    [7] => out of stoc
)

[2] => Array
(
    [0] => book3
    [1] => description3
    [2] => category3
    [3] => code3
    [4] => editor3
    [5] => 0
    [6] => curr3
    [7] => out of stoc
)

[3] => 
)

以及来自第二个 csv 文件的另一个数组(array2):

Array
(
[0] => Array
(
    [0] => book1
    [1] => description1
    [2] => category1
    [3] => code1
    [4] => editor1
    [5] => 9
    [6] => eur
    [7] => out of stoc
)

[1] => Array
(
    [0] => book2
    [1] => description2
    [2] => category2
    [3] => code2
    [4] => editor2
    [5] => 0
    [6] => curr2
    [7] => out of stoc
)

[2] => Array
(
    [0] => book3
    [1] => description3
    [2] => category3
    [3] => code3
    [4] => editor3
    [5] => 12
    [6] => curr3
    [7] => in stoc
)

[3] => 
)

和来自另一个csv的array3:

Array
(
[0] => Array
(
    [0] => book1
    [1] => description1
    [2] => category1
    [3] => code1
    [4] => editor1
    [5] => 10
    [6] => eur
    [7] => in stoc
)

[1] => Array
(
    [0] => book2
    [1] => description2
    [2] => category2
    [3] => code2
    [4] => editor2
    [5] => 0
    [6] => curr2
    [7] => out of stoc
)

[2] => Array
(
    [0] => book3
    [1] => description3
    [2] => category3
    [3] => code3
    [4] => editor3
    [5] => 0
    [6] => curr3
    [7] => out of stoc
)

[3] => 
)

我想知道如何返回一个附加数组(array4),其中包含 [5] 键的最小值,但 0 除外,因为对于数组 Ex 中的每个项目,array1 中的 [5] 键始终为 0:

Array
(
[0] => Array
(
    [0] => book1
    [1] => description1
    [2] => category1
    [3] => code1
    [4] => editor1
    [5] => 10            //  9 < 10, but 9 is not in stock so the next value > 0 is 10
    [6] => eur
    [7] => in stoc
)

[1] => Array
(
    [0] => book2
    [1] => description2
    [2] => category2
    [3] => code2
    [4] => editor2
    [5] => 0           // because this book has 0 price in all arrays, hence the price 
    [6] => curr2       // is not valid
    [7] => out of stoc
)

[2] => Array
(
    [0] => book3
    [1] => description3
    [2] => category3
    [3] => code3
    [4] => editor3
    [5] => 12           // because the only array that contains a valid price is array2
    [6] => curr3
    [7] => out of stoc
)

[3] => 
)
4

2 回答 2

1

试试这个:

$merged = $array1;
foreach($merged as $key => &$value) {
    $prices = array($array1[$key][5], $array2[$key][5], $array3[$key][5]);

    //if all prices are 0, the final price is 0
    if(array_sum($prices) == 0) {
        $value[5] = 0;
    }

    //else find the lowest price in $prices that is > 0
    else {
        $minPrice = PHP_INT_MAX;
        foreach($prices as $price) {
            if($price > 0 && $price < $minPrice) {
                $minPrice = $price;
            }
        }
        $value[5] = $minPrice;
    }
}
unset($value);

print_r($merged);
于 2013-09-16T22:58:25.683 回答
0

如果我理解正确,您希望键 #5 的每个数组的最小值,对吗?

如果是这种情况,这将有所帮助:

$min = array();

foreach ($array_x as $key => $value) {
    if ($value[5] > 0) {
        if (!isset($min[$key])) $min[$key] = $value;
        if ($min[$key] < $value[5]) $min[$key] = $value;
    } //end if
} //end foreach
于 2013-09-17T13:41:22.837 回答