比较 php 中的数组,并通过某些键的值将值从一个分配给另一个


Comparing arrays in php and assign values from one to another by values from certain keys

我已经在我的一个项目上工作了几天,基本上我在这个项目中要做的是比较用户通常在 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,因为对于数组中的每个项目,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] => 
)

试试这个:

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

如果我理解正确,您希望键 #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