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

および 2 番目の 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) を返す方法を知りたいのですが、array1 の [5] キーは配列内の各項目に対して常に 0 になるため、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 に答える