2

My array are $arrIncome and $arrExpense. They have some the same date and some not the same date.

$arrIncome = [
   [
     'date' => '01-01-2019',
     'total' => '500',
   ],
   [
     'date' => '02-01-2019',
     'total' => '200',
   ],
   [
     'date' => '03-01-2019',
     'total' => '300',
   ],
   [
     'date' => '04-01-2019',
     'total' => '900',
   ],
];

$arrExpense= [
   [
     'date' => '01-01-2019',
     'total' => '50',
   ],
   [
     'date' => '02-01-2019',
     'total' => '60',
   ],
   [
     'date' => '07-01-2019',
     'total' => '25',
   ],
   [
     'date' => '08-01-2019',
     'total' => '50',
   ],
];

I loop in $arrIncome array, if I found income date have in $arrExpense array, I will remove an array in $arrExpense by income date of $arrIncome, because I want to make unique date.

foreach ($arrIncome as $income){
        $isExistExpense = array_filter($arrExpense, function($expense) use($income){
            return $expense->date == date('Y-m-d', strtotime($income->date));
        });


        if(count($isExistExpense) > 0 ){
            foreach ($isExistExpense as $expense){
                // THIS PLACE TO UNSET $arrExpense by date value
                unset($arrExpense['date'] = $income->date); // this is a wrong way
            }
        }else{
            // my code more here.....
        }
    }
4
  • What is your question? Commented Feb 24, 2019 at 4:44
  • How to unset array by key and value? Commented Feb 24, 2019 at 4:45
  • Possible duplicate of How to delete an array element based on key? Commented Feb 24, 2019 at 4:46
  • I think it doesn't the same what i want. Array doesn't the same Commented Feb 24, 2019 at 4:48

2 Answers 2

3

You must unset it by the index.

You can do it like:

// Get the intersection of the dates 
$isExistExpense = array_intersect(
    array_column($arrIncome,'date'), 
    array_column($arrExpense,'date'));
// Loop through the `$arrExpense` and unset the that exist in the array. 
foreach($arrExpense as $index=>$vals){
    if(in_array($vals['date'], $isExistExpense)){
        unset($arrExpense[$index]);
    }
}

Hope this helps,

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

3 Comments

It's weird. You have a typo ($v should be $vals) but the code works anyway...
Updated. That is weird.
Actually I was wrong, the code wasn't working with $v it was just that I was testing it on my pre-filtered version of $arrExpense so it wasn't actually doing anything ($isExistExpense was empty).
2

You can use array_filter to directly remove the elements of $arrExpense that have dates which exist in $arrIncome (using array_column to get the list of dates in that array):

$arrExpense = array_filter($arrExpense, function ($v) use ($arrIncome) { 
    return !in_array($v['date'], array_column($arrIncome, 'date'));
});
print_r($arrExpense);

Output:

Array (
   [2] => Array ( [date] => 07-01-2019 [total] => 25 )
   [3] => Array ( [date] => 08-01-2019 [total] => 50 )
)

Demo on 3v4l.org

5 Comments

Yeah, that's better
Your answer is not remove array, but you just show different array on $arrExpense.
Your answer is not remove array, but you just show different array on $arrExpense.
@MengsengOeng the end result is the same, so does it matter?
Because I will to do more in $arrExpense. I must remove element of $arrExpense. Thank for answer.

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.