Alex
Alex

Reputation: 21

How to exclude from multidimentional array values on condition given in another array

I have two arrays where Array1 keys are [teacher_id] in Array2:

Array1 ( [20] => 37 [44] => 40 )

Array2
(
    [0] => Array
        (
            [teacher_id] => 44
            [course_id] => 1180
            [student_id] => 1662
        )

    [1] => Array
        (
            [teacher_id] => 20
            [course_id] => 1180
            [student_id] => 1662
        )
    [2] => Array
        (
            [teacher_id] => 44
            [course_id] => 1180
            [student_id] => 1705
        )
)

I need to exclude this part from Array2:

[0] => Array
    (
        [teacher_id] => 44
        [course_id] => 1180
        [student_id] => 1662
    )

on condition that in array1 value 37 < value 40.

I tried array_filter but I can't figure out how to write proper function for that. Thank you.

Upvotes: 2

Views: 75

Answers (3)

Sultan
Sultan

Reputation: 647

Try this:

$array1 = array('20' => 37, '44' => 40);
$array2 = array(array
        (
        'teacher_id' => 44,
        'course_id' => 1180,
        'student_id' => 1662
    ),
    array
        (
        'teacher_id' => 20,
        'course_id' => 1180,
        'student_id' => 1662
    ),
    array
        (
        'teacher_id' => 44,
        'course_id' => 1180,
        'student_id' => 1705
        ));
$final_array = array_filter($array2, function($item) use ($array1) {
    return $array1[$item['teacher_id']] != max($array1);
});

Upvotes: 1

slevy1
slevy1

Reputation: 3820

If you wish to remove the first element which is an array of the second array, based on a condition concerning the first array, you may array_shift() it off as follows:

<?php
$arr1 = [20 => 37, 44 => 40];
$arr2 = [0 => [
            "teacher_id" => 44,
            "course_id" => 1180,
            "student_id" => 1662],

         1 => [
            "teacher_id" => 20,
            "course_id" => 1180,
            "student_id" => 1662
         ],
         2 => [
            "teacher_id" => 44,
            "course_id" => 1180,
            "student_id" => 1705
         ]];

/* 
remove first element in $arr2 so that teacher_ids commence with first key in first array
provided its value is less than the next one in $arr1 
*/

$arrvalues = array_values( $arr1 );
$i=0;
if ( $arrvalues[$i] < $arrvalues[$i+1] ) {
      array_shift( $arr2 );
      var_dump($arr2);
}

See live code here

The function array_values() facilitates working with values in an associative array. Alternatively, you may unset the first element in $arr2; see this example. Also, you may effectively remove that first element when the condition is met by taking a slice of the array; see here. If you are still determined to use array_filter(), see this example.

Upvotes: 1

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21681

Like this

$array1 = array ( '20' => 37, '44' => 40 );       


$array2 = array (
    0 => 
    array (
        'teacher_id' => 44,
        'course_id' => 1180,
        'student_id' => 1662
    ),
    1 => 
    array (
        'teacher_id' => 20,
        'course_id' => 1180,
        'student_id' => 1662
    ),
    2 => 
    array (
        'teacher_id' => 44,
        'course_id' => 1180,
        'student_id' => 1705
    )
);



$array = array_filter($array2, function($item) use ($array1){
    $key = $item['teacher_id'];

    if(!isset($array1[$key])) return true; //always keep these
    return $item['teacher_id'] > $array1[$key];
});

print_r($array);

Output

Array
(
    [0] => Array
        (
            [teacher_id] => 44
            [course_id] => 1180
            [student_id] => 1662
        )

    [2] => Array
        (
            [teacher_id] => 44
            [course_id] => 1180
            [student_id] => 1705
        )

)

Sandbox

Notice the use ($array1) but what you intend to do with it after that is a mystery.

I am not sure if this

I need to exclude this part from Array2:

Means to keep only that or remove only that. Your question is very ambiguous I don't know what this I need to exclude this part from Array2 .. on condition that in array1 value 37 < value 40. means

Let me re-word that.

You need to exclude some bit, from $array2 on the condition of $array1 [something] value 37 is less then [something] value 40.

And I guess this means

 I have two arrays where Array1 keys are [teacher_id] in Array2:

That 20 => 37 (from $array1) goes with 'teacher_id' => 20 (from $array2)

But how do I do on condition that in array1 value 37 < value 40 on that? Neither of those values come from $array2.

Upvotes: 1

Related Questions