0

I have the following arrays in PHP: print_r($employees) =

Array
(
    [0] => Array
        (
            [p_id] => T29083999
            [name] => Robert Plaxo
        )

    [1] => Array
        (
            [p_id] => T29083388
            [name] => Yvan Sergei
        )

    [2] => Array
        (
            [p_id] => T21083911
            [name] => Rhonda Saunders
        )

    [3] => Array
        (
            [p_id] => H02910382
            [name] => Miguel Mercado
        )
)

then this array: print_r($record) =

Array
(
    [0] => Array
        (
            [c_id] => 1
            [section] => 1061
            [term] => 201631
            [p_id] => T29083388
            [c_date] => 2016-04-01 09:14:00
        )

)

I want to remove the array element from $employees that matches the p_id of $record. Array $record may have multiple entries like the one shown. If so, any p_id in $record must be removed from $employees.

I tried:

foreach ($employees as $k => $e) {
    foreach ($record as $r) {
        if ($e['p_id']==$r['p_id']) {
            echo "found it!";
            // if I uncomment the next line, it crashes! (500 server error)
            //  unset $employees[$k];
        }           
    }   
}

I just want to remove any element from $employees that has any employee that matches any record in $record with that employee id.

2
  • Also tried using: unset $e; with same result (500 server error). Please help! Commented Apr 29, 2016 at 21:05
  • why? I just need the key element to delete from the $employees array. I do not need the key for the comparison array (AFAIK). Commented Apr 29, 2016 at 21:17

2 Answers 2

2

You were almost there; just needed parens around your unset()

I also took the liberty to change some of your variable names as single character variable names bother me.

$employees[] = [
    'p_id' => 'T29083999',
    'name' => 'Robert Plaxo',
];
$employees[] = [
    'p_id' => 'T29083388',
    'name' => 'Yvan Sergei',
];
$employees[] = [
    'p_id' => 'T21083911',
    'name' => 'Rhonda Saunders',
];
$employees[] = [
    'p_id' => 'H02910382',
    'name' => 'Miguel Mercado',
];

$records[] = [
        'c_id' => '1',
        'section' => '1061',
        'term' => '201631',
        'p_id' => 'T29083388',
        'c_date' => '2016-04-01 09:14:00',
];

foreach ($employees as $key => $employee) {
    foreach ($records as $record) {
        if ($employee['p_id'] == $record['p_id']) {
            echo "found it!";
            unset($employees[$key]);
        }
    }
}

echo "<pre>";
print_r($employees);

Outputs

found it!

Array
(
    [0] => Array
        (
            [p_id] => T29083999
            [name] => Robert Plaxo
        )

    [2] => Array
        (
            [p_id] => T21083911
            [name] => Rhonda Saunders
        )

    [3] => Array
        (
            [p_id] => H02910382
            [name] => Miguel Mercado
        )

)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you!!! Finally works! I got all messed up with the array, I have checked everything on the Internet and was going crazy with this!
@user200956 glad to have helped, feel free to upvote and/or accept the answer if it solved your problem
1

The short solution using array_column and array_filter functions. It will also fit your requirement "Array $record may have multiple entries":

$p_ids = array_column($record, "p_id");
$employees = array_filter($employees, function($v) use($p_ids){
    return !in_array($v["p_id"], $p_ids);     
});

print_r($employees);

The output:

Array
(
    [0] => Array
        (
            [p_id] => T29083999
            [name] => Robert Plaxo
        )

    [2] => Array
        (
            [p_id] => T21083911
            [name] => Rhonda Saunders
        )

    [3] => Array
        (
            [p_id] => H02910382
            [name] => Miguel Mercado
        )
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.