【问题标题】:PHP: How to ensure, an array contains all combinations of valuesPHP:如何确保数组包含值的所有组合
【发布时间】:2015-09-23 10:22:08
【问题描述】:

好的,问题来了:我有一个结构如下的数组:

$array[0] 'fruit' -> 'apple', 'origin' -> 'usa', 'price' -> '$1'
$array[1] 'fruit' -> 'apple', 'origin' -> 'canada', 'price' -> '$1'
$array[2] 'fruit' -> 'pear', 'origin' -> 'usa', 'price' -> '$1'
$array[3] 'fruit' -> 'peach', 'origin' -> 'spain', 'price' -> '$2'
$array[4] 'fruit' -> 'peach', 'origin' -> 'greece', 'price' -> '$0.5'

这个数组包含很多水果,有很多不同的来源。目标是确保每个水果都存在所有可能的(= all in the array contained origins) 来源。如果缺少其中一个组合,则应在数组中添加一条新记录,其中包含'price' -> 'tba'。

所以,在上面的例子中,有三种不同的水果(apple, pear, peach) 和四种不同的来源(usa, canada, spain, greece)。

我需要检查每种水果是否有所有可能的来源,如果没有,请将它们与 tba 价格一起添加。

我该如何解决这个in a performant way?我已经尝试使用仅包含起源和水果的附加数组,但到目前为止还没有运气。

编辑:这是预期的结果(目前排序并不重要):

$array[0] 'fruit' -> 'apple', 'origin' -> 'usa', 'price' -> '$1'
$array[1] 'fruit' -> 'apple', 'origin' -> 'canada', 'price' -> '$1'
$array[2] 'fruit' -> 'apple', 'origin' -> 'spain', 'price' -> 'tba'
$array[3] 'fruit' -> 'apple', 'origin' -> 'greece', 'price' -> 'tba'
$array[4] 'fruit' -> 'pear', 'origin' -> 'usa', 'price' -> '$1'
$array[5] 'fruit' -> 'pear', 'origin' -> 'canada', 'price' -> 'tba'
$array[6] 'fruit' -> 'pear', 'origin' -> 'spain', 'price' -> 'tba'
$array[7] 'fruit' -> 'pear', 'origin' -> 'greece', 'price' -> 'tba'
$array[8] 'fruit' -> 'peach', 'origin' -> 'usa', 'price' -> 'tba'
$array[9] 'fruit' -> 'peach', 'origin' -> 'canada', 'price' -> 'tba'
$array[10] 'fruit' -> 'peach', 'origin' -> 'spain', 'price' -> '$2'
$array[11] 'fruit' -> 'peach', 'origin' -> 'greece', 'price' -> '$0.5'

到目前为止我尝试了什么,但没有奏效,因为 a) 变量错误,b) 已达到 30 秒的超时:

$fruits = array('apple', 'pear', 'peach');
$origins = array('usa', 'canada', 'spain', 'greece');

然后我尝试遍历主数组并检查每个组合:

foreach ($fruits as $f)
{
   foreach ($origins as $o)
   {
      if (!current(array_filter($array, function($item) { return $f == $item['fruit'] && $o == $item['origin']; }));)
      {
          // add the fruit-origin combination the the main array
      }
   }
}

这不起作用,因为 $f 和 $o 在函数中不可用。

【问题讨论】:

  • 为什么“到目前为止没有运气”?当你尝试这个时,你得到了什么结果?请给我们看看
  • 添加了预期的结果,以及我到目前为止所尝试的。
  • 为什么希腊的桃子比西班牙的便宜?
  • +1 原因很简单,这个问题突出了 PHP 数组 NOT 多维的事实!

标签: php associative


【解决方案1】:

我不确定您是如何使用此列表的,但您可能希望获得来自特定产地的特定水果的价格,而不必每次都检查整个数组。

为此,使用不同的数组格式是个好主意。你可以像这样生成它:

<?php
$produceList = [
    ['fruit' => 'apple', 'origin' => 'usa',    'price' => '$1'],
    ['fruit' => 'apple', 'origin' => 'canada', 'price' => '$1'],
    ['fruit' => 'pear',  'origin' => 'usa',    'price' => '$1'],
    ['fruit' => 'peach', 'origin' => 'spain',  'price' => '$2'],
    ['fruit' => 'peach', 'origin' => 'greece', 'price' => '$0.5'],
];

$newProduceList = array();
foreach ($produceList as $product) {
    $newProduceList[$product['fruit']][$product['origin']] = $product['price'];
}

这将为您提供一个数组,您可以在其中使用水果和原产地作为索引:

Array(
    [apple] => Array(
            [usa] => $1
            [canada] => $1
    )
    [pear] => Array(
            [usa] => $1
    )
    [peach] => Array(
            [spain] => $2
            [greece] => $0.5
    )
)

这样很容易获得产品的具体价格:

<?php
if (isset($newProduceList['apple']['germany'])) {
    echo $newProduceList['apple']['germany'];
} else {
    echo 'tba';
}
// Output: tba

如果您在这段代码周围添加一个函数作为包装器,则可以使用“getProducePrice('apple', 'canada');”返回价格或“tba”。

<?php
function getProducePrice($fruit, $origin) {
    global $newProduceList; // it's bad style to use globals, used just as example
    if (isset($newProduceList[$fruit][$origin])) {
        return $newProduceList[$fruit][$origin];
    } else {
        return 'tba';
    }
}

echo getProducePrice('pear', 'usa');
// Output: $1

使用原始数组

如果您坚持使用原始数组格式,您总是必须遍历数组来搜索特定的水果,如下所示:

<?php
$price = 'tba';
foreach ($produceList as $produce) {
    if ($produce['fruit']!='peach' || $produce['origin']!='spain') continue;
    // found the specific fruit from the specific origin
    $price = $produce['price'];
    break; // stop iterating through the rest of the array.
}
echo $price;
// Output: $2

【讨论】:

    【解决方案2】:

    看来我找到了解决办法:

    if (!current(array_filter($array, function($item) use($f, $o) { return $f == $item['fruit'] && $o == $item['origin']; }));)
          {
              // add the fruit-origin combination the the main array
          }
    

    我错过了use($f, $o) 部分。我不确定这是否可以明智地调整性能,但目前我猜它已经足够好了。但是,总是欢迎改进:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 2022-11-03
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      相关资源
      最近更新 更多