douli1306 2013-06-20 12:13
浏览 7
已采纳

PHP Logic,测试数组的值

I've got a slight problem with figuring out the logic for the following problem.

I have a webshop which sells ankle braces. Left and Right. For each brace a scan of the corresponding ankle is neccesary. So, for example, I cannot buy a left brace if I only have a scan of the right ankle. I have two arrays. First array contains the scan data and the second is the web cart containing all the products. But I can't wrap my head around the logic:

return false if a product is in the cart while its scan is not available.

I could just do a bunch of if statements but i'm certain that there is a logical and cleaner way.

Here are the arrays

Array
(
    [left] => Array
        (
            [0] => data
            [1] => data2
            [2] => data3
        )

    [right] => Array
        (
            [0] => data
            [1] => data2
            [2] => data3
        )

)

Array
(
    [product_id1] => Array
        (
            [var] => val
            [side] => left
        )

    [product_id2] => Array
        (
            [var] => val
            [side] => right
        )

)
  • 写回答

1条回答 默认 最新

  • dongqiyang3711 2013-06-20 12:23
    关注

    Something like this might help:

    function check($product) {
        if ($product['side'] == 'left' && !isset($scans['left']) || 
            $product['side'] == 'right' && !isset($scans['right'])) {
              return false;
            }
        return true;
    }
    
    foreach ($cart as $product) {
      check($product);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?