【问题标题】:Comparing arrays in php and assign values from one to another by values from certain keys [closed]比较php中的数组并通过某些键的值将值从一个分配到另一个[关闭]
【发布时间】:2013-09-16 22:50:37
【问题描述】:

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

【问题讨论】:

    标签: php excel csv multidimensional-array


    【解决方案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);
    

    【讨论】:

    • 感谢您提供的解决方案,它就像一个梦想,但不幸的是我发现了我的逻辑中的一个漏洞,因为在我的项目中,array1 将始终具有 [5] key = 0,因此最终过程将始终是0,所以我编辑了问题以符合正确的逻辑。
    • 我做了一些改变;再来一次。
    • 做到了,第三个数组的价格是 0 insted of 12
    • 我需要一些东西来处理所有数组中 [5] 的值,如果该值为 0,那么 [5] 的最终值也将为 0,但如果有任何一个或多个值将是> 0 然后对这些值进行最小值并输出 [5] 的右端值。
    • 哦,我明白了。您不仅要忽略$array1;你想忽略所有的 0,除非所有的价格都是 0。明白了。已编辑。
    【解决方案2】:

    如果我理解正确,您希望键 #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-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 2013-11-13
      相关资源
      最近更新 更多