Remove Specific key value pair from PHP array

372

I have two key value pair arrays, one is the original, the other an array of key value pairs that need to be removed. I need to remove a specific combination, ie $removeArray would contain:

Array([Word] => 78)

I've tried:

foreach($removeArray as $key => $value){unset($originalArray[$key][$value]);}

This doesn't work at all. I need to remove based off an exact key value pair match.


EDIT:

Original

Array ( [distribution] => 25 [watch] => 25 [electricity] => 25 [timepiece] => 8 [wristwatch] => 25 [energy] => 8 [transmission] => 8 [clock] => 16 ) 

Remove

Array ( [timepiece] => 8 [energy] => 8 [watch] => 17 ) 

Result

Array ( [distribution] => 25 [watch] => 25 [electricity] => 25 [wristwatch] => 25  [transmission] => 8 [clock] => 16 ) 

NOTE:

[watch] => 25 is not affected, because it is not equal to[watch] => 17

772

Answer

Solution:

try this

foreach($removeArray as $key => $value){
    if($value==$originalArray[$key])
        unset($originalArray[$key]);
}
512

Answer

Solution:

You can check like:

if($originalArray[$key] == "78" && $key = "Word") {
unset($originalArray[$key]);
}

People are also looking for solutions to the problem: javascript - Server blocks POST but not GET requests?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.