返回

php-使用多维数组获取条件发生数

发布时间:2022-07-05 15:51:15 176
# 后端

我想数一数$items_array进入$condition_array基于item_idquantity

1, 'quantity'=>1],
        ['item_id'=>11, 'quantity'=>3],
        ['item_id'=>12, 'quantity'=>6]
    ];

    $condition_array = [
        ['item_id'=>11, 'quantity'=>1],
        ['item_id'=>12, 'quantity'=>2]
    ];

    $count = ?; // Should equal 3
?>

在本例中$count应该是3,因为两者都是$condition_array item_id的存在于$item_array各自的量相互作用3次(6/2和3/1)。

进一步示例显示了所需的$count基于此逻辑的值:

12, 'quantity'=>2]
    ];

    $condition_array = [
        ['item_id'=>11, 'quantity'=>1],
        ['item_id'=>12, 'quantity'=>2]
    ];

    $count = ?; // Should equal 0 (because item_id 11 missing from $items_array)
?>


1, 'quantity'=>1],
        ['item_id'=>11, 'quantity'=>2],
        ['item_id'=>12, 'quantity'=>1]
    ];

    $condition_array = [
        ['item_id'=>11, 'quantity'=>1],
        ['item_id'=>12, 'quantity'=>2]
    ];

    $count = ?; // Should equal 0 (because all the quantities don't go in to each other (item_id 12 1/2))
?>


12, 'quantity'=>2],
        ['item_id'=>11, 'quantity'=>1]
    ];

    $condition_array = [
        ['item_id'=>11, 'quantity'=>1],
        ['item_id'=>12, 'quantity'=>2]
    ];

    $count = ?; // Should equal 1 (exact match)
?>

1, 'quantity'=>4],
        ['item_id'=>12, 'quantity'=>1],
        ['item_id'=>11, 'quantity'=>1]
    ];

    $condition_array = [
        ['item_id'=>1, 'quantity'=>2],
        ['item_id'=>12, 'quantity'=>1]
    ];

    $count = ?; // Should equal 1 (because despite item_id 1 going in to $condition_array twice (4/2), item_id 12 only has 1 quantity in $items_array, if item_id 12  had a quantity of 2 in $item_array then $count would equal 2)
?>

https://www.php.net/manual/en/ref.array.php但似乎找不到用于此的正确函数。

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像