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

时间:2013-09-16 22:50:38

标签: php excel csv multidimensional-array

我已经在我的一个项目上工作了几天,基本上我在这个项目中尝试做的是比较用户通常在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] => 
)

我想知道如何返回一个带[5]键最小值的附加数组(array4),除了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] => 
)

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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