2

i have 2 arrays i need to compare exactly in the same order, i need to check if each array from classes subA and subB contains the value from the one in prerequisite.

my prerequisite array

Array
(
    [s1] => MAT
    [s2] => ENG
    [s3] => PHY
    [s4] => CHE
    [s5] => FRE
)

and my classes array

Array
(
    [subA] => MAT
    [subB] => ENG
)
Array
    (
     [subA] => ENG
     [subB] =>
    )
Array
    (
     [subA] => MAT
     [subB] => PHY
    )
Array
    (
     [subA] => CHE
     [subB] => 
    )
Array
    (
     [subA] => MAT
     [subB] => FRE
    )

my code

$prerequisite = array(
  's1' => 'MAT',
  's2' => 'ENG',
  's3' => 'PHY',
  's4' => 'CHE',
  's5' => 'FRE'
);

$confirmed = TRUE;

$i = 0;
foreach($prerequisite as $p )
{                     
  if( in_array( $prerequisite, $classes[$i] ) )
  {
    // The prerequisite has been met
  }
  else
  {
    // The prerequisite has not been met
    $confirmed = FALSE;
  }
  $i++;
}

if($confirmed == TRUE) { //run insert query here }

expected output

I need to return TRUE if all prerequisites are met, need it to find for example:

  • if first array from classes subA or subB matches s1 value MAT from prerequisite array
  • if second array from classes subA or subB matches s2 value ENG from prerequisite array
  • if third array from classes subA or subB matches s3 value PHY from prerequisite array
  • if fourth array from classes subA or subB matches s4 value CHE from prerequisite array
  • if fifth array from classes subA or subB matches s5 value FRE from prerequisite array

Problem

I am always getting FALSE and not being able to run the insert query

mirvatJ
  • 366
  • 1
  • 3
  • 15

3 Answers3

1

You're not using $p, you're providing an array as the needle argument to in_array(): http://php.net/manual/en/function.in-array.php

If you replace the in_array needle with $p, you'll be searching $classes[$i] for a single value rather than an entire array.

You also don't seem to need that entire block, just use a ! (not) on the condition rather than checking a condition and using an else block.

if( ! in_array( $p, $classes[$i] ) )
{
    $confirmed = false;
}
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
1

This question provides a beautiful use case for the PHP function array_map():

$prerequisite = array(
  's1' => 'MAT',
  's2' => 'ENG',
  's3' => 'PHY',
  's4' => 'CHE',
  's5' => 'FRE'
);

$classes = array(
    array(
        'subA' => 'MAT',
        'subB' => 'ENG',
    ),
    array(
        'subA' => 'ENG',
        'subB' => NULL,
    ),
    array(
        'subA' => 'MAT',
        'subB' => 'PHY',
    ),
    array(
        'subA' => 'CHEx',        // <---- doesn't match the prerequisite
        'subB' => NULL,
    ),
    array(
        'subA' => 'MAT',
        'subB' => 'FRE',
    ),
);

// array_filter() removes the matching classes
$missing = array_filter(
    array_map(
        function(array $class, $pre, $key) {
            // Return $key when the values of both 'subA' and 'subB'...
            // ... are different than the expected prerequisite
            return ($class['subA'] !== $pre && $class['subB'] !== $pre) ? $key : NULL;
        },
        $classes,
        $prerequisite,
        array_keys($prerequisite)
    )
);


// $missing associates the keys of $classes (numbers) to the keys of $prerequisite
// ... for classes that do not match their prerequisite
print_r($missing);

The output is:

Array
(
    [3] => s4
)
axiac
  • 68,258
  • 9
  • 99
  • 134
0

This can be an possible duplicate of: Checking to see if one array's elements are in another array in PHP

You should use array_intersect for this approach...

I will answer because there are also another things that you haven't cleared.

$prerequisite = array(
  's1' => 'MAT',
  's2' => 'ENG',
  's3' => 'PHY',
  's4' => 'CHE',
  's5' => 'FRE'
);

$classes1 = array(
    "subA" => "MAT",
    "subB" => "ENG"
);

$classes2 = array(
    "subA" => "ENG",
    "subB" => ""
);

if(!empty(array_intersect($prerequisite, $classes1))) { 
    //run insert query here
}

You have missed some things:

  • You have used $classes as a numeric array instead of an associative one.
  • You have missed $p in the foreach

As you can see, I have created the array from the query that you have told me.

You can see this working on here

z3nth10n
  • 2,341
  • 2
  • 25
  • 49