数组中的Foreach值检查该值是否大于另一个数组中的任何值

时间:2014-12-10 17:41:08

标签: php arrays

我有两个数组,其中键是[part id]。值为record =>数量:长度。我希望能够检查来自sencond数组的同一部分id下的任何值是否大于第一个数组的长度。例如,第二个数组中的Part id 2099的qty:length为14:11.25,并且大于6:3.33,我希望它在PHP中返回true。我有一个功能来分割数量和长度但在那之后我不确定去哪里。它返回"警告:explode()期望参数2为字符串,"任何帮助赞赏。

Array
(
    [2099] => Array
        (
            [360] => 6:3.33
            [362] => 14:8.75
        )

    [2130] => Array
        (
            [361] => 4:2.5
        )

)
Array
(
    [2099] => Array
        (
            [360] => 12:8.33
            [362] => 14:11.25
        )

    [2130] => Array
        (
            [361] => 24:3.5
        )

)

我的PHP:

foreach ($a as $partid=>$qty_length){
    $ex_part = explode(":", $qty_length);
}

2 个答案:

答案 0 :(得分:1)

您可以执行嵌套foreach循环来查找差异

<?php

$array1 = array(2099 => array(360 => "6:3.33",362 => "14:8.75"),2130 => array(361 => "4:2.5"));
$array2 = array(2099 => array(360 => "12:8.33",362 => "14:11.25"),2130 => array(361 => "24:3.5"));

foreach($array1 as $key => $value)
{
    foreach ($value as $key1 => $value1) {

        // $array1[$key][$key1] get the value of array one curreny key
        // $array2[$key][$key1] get the value of array two current key

        $one = explode(':',$array1[$key][$key1]); // array one value e.g 360 => "6:3.33"
        $two = explode(':',$array2[$key][$key1]); // array two value e.g 360 => "12:8.33"


        // do what ever you want here 
        if($one[0] > $two[0])
        {
            echo "array one key " . $key1 . " is bigger <br>";
        }else{
            echo "array two key " . $key1 . " is bigger <br>";
        }
    }
}   

答案 1 :(得分:0)

$arr =Array
(
        '2099' => Array
        (
                '360' => '6:3.33',
                '362' => '14:8.75'
        ),

        '2130' => Array
        (
                '361' => '4:2.5'
        ),
        '2131' => Array
        (
                '362' => '24:3.5'
        )

);
$arr1=Array
(
        '2099' => Array
        (
                '360' => '12:8.33',
                '362' => '14:11.25'
        ),

        '2130' => Array
        (
                '361' => '24:3.5'
        ),
        '2131' => Array
        (
                '362' => '20:3.5'
        )

);





foreach ($arr as $partid=>$qty_length){

        foreach($qty_length as $key=>$val){
            $ex_part = explode(":", $val);
        //  print_r($ex_part);
            $ex_part1 = $arr1[$partid];
            $len = sizeof($ex_part1);
        foreach( $ex_part1 as $key1=>$val1){    
            $ex_part1 = explode(":", $arr1[$partid][$key1]);
            if($ex_part[0] < $ex_part1[0] && $ex_part[1] < $ex_part1[1]){
                $len--;
                if($len == 0){
                    echo $ex_part1[0]."<br>";
                }else{

                    continue;
                }


            }else if($ex_part[0] >= $ex_part1[0]  && $ex_part[1] >= $ex_part1[1]){

            $len--;
                if($len == 0){
                    echo $ex_part[0]."else <br>";
                }else{

                    continue;
                }

            }
        }

        }

    }   

您也可以添加更多条件。