在PHP中比较数组并通过某些键的值将值从一个指定给另一个[关闭]

I have been working on a project of mine for a few days now, and essentially what I'm trying to do in this project is a comparison of csv file that a user normally does in excel, to do it in php automatically every night. I got the info from the CSV's intro arrays, but I'm having trouble combining them to get the right info for every book, hence the following example and question.

I have the following array (array1) from a csv file, in witch the [5] key will always be 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] => 
)

and another array (array2) from a second csv file :

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] => 
)

and array3 from yet another csv:

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] => 
)

I would like to know how to return an additional array (array4) with the minimum values for [5] key, except 0 because the [5] key from array1 will always be 0, for each of the items in the array Ex:

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] => 
)

Try this:

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

If I understand correctly you want the smallest value per array for key #5, correct?

If that is the case this will help:

$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