0

I have two array as I mention below:

$arr1 = array("1","3","4","6");
$arr2 = array("2","3","4","5","7");

I want to get those elements that are not in $arr2 without using built-in function. So, how can I do this?

6
  • 1
    why not array_diff? Commented Jun 27, 2019 at 11:04
  • I answered a similar question yesterday. See if this helps stackoverflow.com/questions/56771451/array-diff-in-foreach Commented Jun 27, 2019 at 11:05
  • "without using built in function" Do you specifically mean only PHP's array functions? Or all built-in functions? Commented Jun 27, 2019 at 11:18
  • I mean without any function like array_diff(), unset(), array_splice() any other function which is already define in php @JayBlanchard Commented Jun 27, 2019 at 11:21
  • @darkshadow There is no alternative for unset, you must use it. Commented Jun 27, 2019 at 13:18

3 Answers 3

1

You can loop twice and unset if matches and break the inner loop

$arr1 = array("1","3","4","6");
$arr2 = array("2","3","4","5","7");

foreach($arr1 as $k => $v){
    foreach($arr2 as $k1 => $v1){
        if($v == $v1){
            unset($arr1[$k]);break;
        }
    }
}
print_r($arr1);

Demo.

Output:

Array
(
    [0] => 1
    [3] => 6
)
Sign up to request clarification or add additional context in comments.

2 Comments

The OP said "without using built-in function"
Yep - that is almost exactly what I would do. Excellent answer.
1

I was intrigued by the question of not using any of the array functions or specialty functions in PHP, so here is what I came up with to diff the two arrays (as they are written, no further testing has been performed, it would likely break) without using those functions. It requires some juggling:

$arr1 = array("1","3","4","6");
$arr2 = array("2","3","4","5","7");

$inTwo = array();
$notInTwo = array();

// get the matching elements from the two arrays to create a new array
foreach($arr1 AS $key => $val) {
    foreach($arr2 AS $key2 => $val2) {
        if($val == $val2) {
            $inTwo[] = $val2;
        }
    }
}

print_r($inTwo);
$match = NULL; // variable used to hold match values, so they can be skipped

foreach($arr1 AS $key3 => $val3) {
    foreach($inTwo AS $key4 => $val4) {
        echo $val3 . ' ' . $val4 . "\n";
        if(($val3 == $val4) || ($match == $val4)) { // test
            echo "match\n";
            $match = $val3; // set the new variable, to be checked on the next iteration
            echo $match ."\n";
            break;
        } else {
            $notInTwo[] = $val3;
            break;
        }
    }
}

print_r($notInTwo);

Here is the output (all test output left in for reference):

Array //print_r($inTwo);
(
    [0] => 3
    [1] => 4
)
1 3
3 3
match // set the match variable
3
4 3 // skip this due to the match
match // set the match variable
4
6 3
Array print_r($notInTwo);
(
    [0] => 1
    [1] => 6
)

EXAMPLE

Diffing two arrays requires some recursion. If you want to know how this works under the hood you could look into the source for PHP (and other languages that offer a diffing algorithm) to get an idea of how to do this. What I've written here is crude, a bull-in-the-China-shop approach to the problem.

1 Comment

I ain't thought that way+1:):)
0

you can use array_walk with in_array

$r = [];
array_walk($arr1, function($v, $k) use (&$r, $arr2){
  !in_array($v,$arr2) ? ($r[]=$v) : '';
});

1 Comment

The OP said "without built-in functions"

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.