Compare two arrays by position to get a third array

Hi I need help with comparing two arrays and getting this outcome:
1110 0010 0010 1111 1010 1111 1100

Open in new window


These are my arrays:
Array ( [0] => 0101 [1] => 0100 [2] => 0110 [3] => 1000 [4] => 0110 [5] => 0001 [6] => 0111 [7] => 0100 ) 
Array ( [0] => 1011 [1] => 0110 [2] => 0101 [3] => 1010 [4] => 1001 [5] => 1011 [6] => 1000 [7] => 1000 )

Open in new window


As you can see 1&1 or 0&0 = 0 , while 1&0 = 1.
Is there a way to do that and put the answer inside an array by 4's as well? I am using php framework
LVL 1
Jazzy 1012Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
There may be something more elegant out there but this works

$a = Array ( "0101","0100","0110","1000","0110","0001","0111","0100");
$b = Array ( "1011","0110","0101","1010","1001","1011","1000","1000");
$c = [];

for ($x = 0; $x <= count($a)-1; $x++) { // LOOP THROUGH FIRST ARAAY
    
     $first = str_split($a[$x]); // CREATE AN ARRAY OF THE RESULT BY SPLITTING THE STRING  "0101" WILL BE ARRAY("0","1","0","1"0
     $second = str_split($b[$x]);
     
    $string = morph($first[0],$second[0]).morph($first[1],$second[1]).morph($first[2],$second[2]).morph($first[3],$second[3]);  // CONCATENATE
    
    array_push($c,$string); // UPDATE ARRAY $C
        
} 

print_r($c);

function morph($a,$b){  // IF A AND B = 1 THEN RETURN 1 ELSE 0 (0+1 MORPHES TO 1, 1+1 MORPHES TO 0
    if($a+$b===1){
        return 1;
    } else {
        return 0;
    }
}

Open in new window


Your expected result is not totally correct.  It should be
Array ( [0] => 1110 [1] => 0010 [2] => 0011 [3] => 0010 [4] => 1111 [5] => 1010 [6] => 1111 [7] => 1100 )
0
Julian HansenCommented:
What you are looking at is an XOR function
Truth Table for XOR
A    B    A XOR B
-----------------
0    0    0
1    0    1
0    1    1
1    1    0

Open in new window

In PHP the XOR bitwise operator is '^'

You can obtain the results as simply as doing this for each value
$result = $a ^ $b

Open in new window


The trick with this problem is to get the array's into the right format. For instance - if we had this
// Array values stored as binary
$a = array (0b0101 , 0b0100 , 0b0110 , 0b1000 , 0b0110 , 0b0001 , 0b0111 , 0b0100 );
$b = array (0b1011 , 0b0110 , 0b0101 , 0b1010 , 0b1001 , 0b1011 , 0b1000 , 0b1000 );

Open in new window

This is a sample
$c = array();
foreach($a as $i => $v) {
  // XOR them    
  $k = $a[$i] ^ $b[$i];

   // MAKE INTO STRING
  $c[]= sprintf("%04b", $k);
}
// Dump the result
echo "<pre>" . print_r($c, true) . "</pre>";

Open in new window


To adapt this to your specific setup we need to know what the original data looks like and how that must be represented in the final array.

If the arrays are strings then we would need to convert the string to binary which would work like this
$a = array ('0101' , '0100' , '0110' , '1000' , '0110' , '0001' , '0111' , '0100' );
$b = array ('1011' , '0110' , '0101' , '1010' , '1001' , '1011' , '1000' , '1000' );

$c = array();
foreach($a as $i => $v) {
	$k = bindec($a[$i]) ^ bindec($b[$i]);
	$c[]= sprintf("%04b", $k);
}

echo "<pre>" . print_r($c, true) . "</pre>";

Open in new window

1
Jazzy 1012Author Commented:
Is there a way to change this array into Hexadecimal
Array
(
    [0] => 1110
    [1] => 0010
    [2] => 0011
    [3] => 0010
    [4] => 1111
    [5] => 1010
    [6] => 1111
    [7] => 1100
)

Open in new window


So that for example array[1] would be E?
0
Julian HansenCommented:
I don't understand how array[1] becomes 'E'
Array[1] is 0010 which is 2 in decimal and hexadecimal
0
Julian HansenCommented:
Ok I see what you mean - you were talking about array[0] - array's are 0 indexed so array[1] is actually the second item in the array

You are referring to '1110' displaying as its HEX equivalent 'E'

The number remains the same - it is the representation we are interested in so all we do is change the sprintf format specifier to an 'X' to specify a HEX output (capitalised)
$a = array ('0101' , '0100' , '0110' , '1000' , '0110' , '0001' , '0111' , '0100' );
$b = array ('1011' , '0110' , '0101' , '1010' , '1001' , '1011' , '1000' , '1000' );

$c = array();
foreach($a as $i => $v) {
  $k = bindec($a[$i]) ^ bindec($b[$i]);
  // NOTE THE %X => MEANS OUTPUT VALUE AS A HEX DIGIT (CAPITALISED)
  $c[]= sprintf("%X", $k);
}

echo "<pre>" . print_r($c, true) . "</pre>";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.