简体   繁体   English

如何检查其他多维数组中的1个多维数组中的数据

[英]how to check data from 1 multidimensional array in other multidimensional array

I have 2 multidimensional array. 我有2个多维数组。 First one is $voucher_menu will be return to this following array : 第一个是$voucher_menu将返回以下数组:

this one is print_r : 这是print_r:

Array
(
[menu_0] => Array
    (
        [menu_id] => 521
        [qty] => 1
        [choice] => Array
            (
                [0] => 1
                [1] => 6
            )

    )

[menu_1] => Array
    (
        [menu_id] => 525
        [qty] => 2
        [choice] => Array
            (
                [0] => 8
            )

    )

[menu_2] => Array
    (
        [menu_id] => 520
        [qty] => 3
        [choice] => Array
            (
            )

    )

)

And the second one $item_cart will return to this following array : 第二个$item_cart将返回以下数组:

Array
(
[menu_0] => Array
    (
        [menu_id] => 517
        [qty] => 1
        [choice] => Array
            (
                [0] => 11
                [1] => 12
            )

    )

[menu_1] => Array
    (
        [menu_id] => 525
        [qty] => 1
        [choice] => Array
            (
                [0] => 8
            )

    )

[menu_2] => Array
    (
        [menu_id] => 521
        [qty] => 2
        [choice] => Array
            (
                [0] => 2
                [1] => 6
            )

    )

[menu_3] => Array
    (
        [menu_id] => 520
        [qty] => 3
        [choice] => Array
            (
            )

    )

 )

I want trying to validate all data in $voucher_menu from the menu_id , the value of qty and choice should be haved in $item_cart . 我想尝试从menu_id验证$voucher_menu所有数据, qtychoice的值应在$item_cart

This is my code what i have done : 这是我所做的代码:

for($i=0;$i<count($voucher_menu);$i++){
    if(count($voucher_menu["menu_".$i]["choice"]) != 0) {
        $variant_menu[] = $voucher_menu["menu_" . $i]["choice"];
    }
}

    $valid = true;
    foreach($voucher_menu as $row){
        $pass = false;
        foreach($item_cart as $value) {
            if ($row["menu_id"] == $value["menu_id"]) {
                if (isset($variant_menu)) {
                    if(count($row["choice"]) > 0) {
                        $variant = in_array($row["choice"], $value["choice"]);

                    }

                }
                if ($variant == true) {
                    $pass = true;
                    break;
                }
                if ($row["qty"] <= $value["qty"]) {
                    $pass = true;
                    break;
                }

            }
        }


        if(!$pass){
            $valid = false;
            break;
        }
    }

    if($valid == true){
        echo "You got your discount";
    }else{
        echo "You dont get any discount";
    }

I can validate the menu_id and the qty of item but i dont know how to validate the choice . 我可以验证menu_id和项目的qty ,但我不知道如何验证choice

I want if in $voucher_menu and if the menu_id have choice and the array of choice is not 0 , than the value choice should be same in item_cart 我想如果在$voucher_menu并且如果menu_idchoice并且choice array不为0 ,则值choiceitem_cart应该相同

guys can you help me how to check the choice ? 伙计们,您能帮我检查一下choice吗? or if you have the other way to validate data from $voucher_menu to $item_cart please show me. 或者,如果您还有其他方法可以验证从$voucher_menu$item_cart数据,请告诉我。

thank you (: 谢谢 (:

ps the choice string is id and will be return different id if the menu_id are different so if menu_id is 521 the choice is 1, 6 in other menu_id will not 1,6 PS的choice字符串id和将返回不同的id ,如果menu_id是不同的,所以如果menu_id521choice1, 6在其它menu_id不会1,6

Try array_diff or array_intersect 尝试array_diffarray_intersect

With array_diff you can do the following within your loops: 使用array_diff可以在循环中执行以下操作:

$variant = count(array_diff($row["choice"], $value["choice"])) == 0;

You might even experiment with doing an array_diff on the whole array of menus and checking if qty was different, simplifying the code greatly. 您甚至可以尝试对整个菜单数组执行array_diff并检查qty是否不同,从而大大简化了代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM