-6

Please check below array I need to compare values. I have two array one is

Array("Size"=>"Small","Color"=>"Red");

And Second Array is

 Array(Array("Name"=>"Size","Value"=>"Small"),
      Array("Name"=>"Color","Value"=>"Red"),
);

Now I want to compare size of first array with size of second array and color of first array with color of second array. I need both should be match. In short size should be small and color should be red. Please help Thanks in advance.

Ankit
  • 43
  • 1
  • 5

3 Answers3

0

Your code does not seem to make much sense but here:

if ( $array1['Size'] === $array2[0]['Value'] && $array1['Color'] === $array2[1]['Value'] ) { /* code here */ }
malutki5200
  • 1,092
  • 7
  • 15
0

Your question is not so clear, but as per my understanding you want something like this.

$a1 = Array("Size"=>"Small","Color"=>"Red");
$a2 = Array(
            Array("Name"=>"Size","Value"=>"Small"),
            Array("Name"=>"Color","Value"=>"Red"),
      );

foreach($a2 as $k=>$v) {
    if($v['Name'] == "Size") {
        if($v['Value'] == $a1['Size']) {
            // Do you stuff
        }
    }
    else if($v['Name'] == "Color") {
        if($v['Value'] == $a1['Color']) {
            // Do you stuff
        }
    }
}
TIGER
  • 2,864
  • 5
  • 35
  • 45
0

Please note that the function you are searching for is a validator.

Just coded a simple on for you :

$data      = array(
    'size' => 'small',
    'color' => 'red',
);
$validator = array(
    array(
        'name' => 'size',
        'value' => 'small',
    ),
    array(
        'name' => 'color',
        'value' => 'red',
    ),
);


function validateData(array $data, array $validator, $validateSize = false)
{
    if( $validateSize )
    {
        if( (count($data) != count($validator)) )
        {
            return false;
        }

        $found  = array_keys($data);
        $toFind = array();
        foreach($validator as $vI)
        {
            $toFind[] = $vI['name'];
        }
        $toFind = array_unique($toFind);

        if( count($found) != count($toFind) )
        {
            return false;
        }
    }

    $validate = function($name, $val) use ($validator)
    {
        $return = null;

        foreach($validator as $vItem)
        {
            if( $vItem['name'] === $name )
            {
                if( $vItem['value'] === $val )
                {
                    if($return !== false)
                    {
                        $return = true;
                    }
                }
                else
                {
                    $return = false;
                }
            }
        }

        if( is_null($return) )
        {
            $return = false;
        }

        return $return;
    };

    foreach($data as $k => $v)
    {
        if( !$validate($k, $v) )
        {
            return false;
        }
    }

    return true;
}

This will return true if you data array contains only key/value pairs allowed in $validator, false otherwise.

You can also validate that all keys are present in the data sent by setting $validateSize to true.

Hope it helped.

Bobot
  • 1,118
  • 8
  • 19