• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 40
  • Last Modified:

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
0
Jazzy 1012
Asked:
Jazzy 1012
  • 3
2 Solutions
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now