Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

PHP Array comparing help.


fashizzlepop's Avatar
Member
0 0

I need help comparing 2 arrays in PHP. I have one array of 5 md5 hashes and 1 array of a md5 hash of every letter a-z A-Z and 1-9 and 0.

I need to take the first array and compare it to the second one so it tells me which letter or number the md5 is from and then echo it. I have tried so many things it seems that the world just doesn't want me to know. I have googled and stuff but am just that 1 step away. here is what I got so far…

foreach ($list1 as $a)
   {
  foreach ($list2 as $b)
      {
           if ($a == $b)
             {
               echo "$b";
             }
       }
   }
?>```


ghost's Avatar
0 0

I can't see what's wrong with your code, but I tried this and it worked:

<?php
$list2=array("hash1","hash2","hash3","hash4","hash5");
$list1=array("wrong1","wrong2","wrong3","hash1","hash4");
foreach($list1 as $a)
{
        foreach($list2 as $b)
        {
                if($b==$a)
                {
                        echo "$a\n";
                }
        }
}
?>

It's apparently the same code.

Edit: Maybe I'm not understanding your question right.


Mr_Cheese's Avatar
0 1

that should work.

might want to add a break; in there to save resources.

only problem i can see arrising is if your arrays are like this:

$array['as234ad432asad4343asda43343ad'] = 'a';

then that will cause problems, because you'll be checking the md5 against 'a' instead of another hash.

for that situation use: foreah($array as $name => $value){ } $name = as234ad432asad4343asda43343ad $value = a


fashizzlepop's Avatar
Member
0 0

Hmmm, maybe I didn't turn my array into hashes correctly. Here's my whole code. The point is to take a 160 char long string and break it into 5 md5 hashes. Then compare it to what 5 separate characters generated those 5 hashes.

<?php
$thetext = $_POST['hash'];

$newtext = wordwrap($thetext, 32, ",", true);

$hashes = array(explode(",", $newtext));

function sporks($n)
{

$m = md5($n);

return($m);
}

$hashed = array_map("sporks", $values);


foreach ($hashes as $a)
   {
  foreach ($hashed as $b)
      {
           if ($a == $b)
             {
               echo "$b";
             }
       }
   }
?>

And the POST works fine for sure.


ghost's Avatar
0 0

Uhm, you didn't define $values.

Edit: If you do print_r($hashed), you'll see that it's empty.


fashizzlepop's Avatar
Member
0 0

Sorry I forgot to post ALL my code. Here it is…

$values = array('0','1','2','3','4','5','6','7','8','9','a','A','b','B','c','C','d','D','e','E','f','F','g','G','h','H','i','I','j','J','k','K','l','L','m','M','n','N','o','O','p','P','q','Q','r','R','s','S','t','T','u','U','v','V','w','W','x','X','y','Y','z','Z'); 

$thetext = $_POST['hash'];

$newtext = wordwrap($thetext, 32, ",", true);

$hashes = array(explode(",", $newtext));

function modeish($n)
{

$m = md5($n);

return($m);
}

$hashed = array_map("modeish", $values);


foreach ($hashes as $a)
   {
  foreach ($hashed as $b)
      {
           if ($a == $b)
             {
               echo "$b";
             }
       }
   }
?>```

I had $values up higher at the beggining of the page.

ghost's Avatar
0 0

$hashes is 2-dimensional. I'll see if I can figure out how to make it a 1-dimensional array.

Edit:

<?php
$values = array('0','1','2','3','4','5','6','7','8','9','a','A','b','B','c','C','d','D','e','E','f','F','g','G','h','H','i','I','j','J','k','K','l','L','m','M$
$thetext = $_GET['hash'];

$newtext = wordwrap($thetext, 32, ",", true);
$hashes = explode(",", $newtext);

function modeish($n)
{

$m = md5($n);

return($m);
}

$hashed = array_map("modeish", $values);
foreach ($hashes as $a)
{
foreach ($hashed as $b)
{
if ($a == $b)
{
echo "$b";
}
}
}
?>

That should work.


fashizzlepop's Avatar
Member
0 0

Heres my new code and here is my error…

$values = array('0','1','2','3','4','5','6','7','8','9','a','A','b','B','c','C','d','D','e','E','f','F','g','G','h','H','i','I','j','J','k','K','l','L','m','M$
$thetext = $_GET['hash'];

$newtext = wordwrap($thetext, 32, ",", true);
$hashes = explode(",", $newtext);

function modeish($n)
{

$m = md5($n);

return($m);
}

$hashed = array_map("modeish", $values);
foreach ($hashes as $a)
{
foreach ($hashed as $b)
{
if ($a == $b)
{
echo "$b";
}
}
}
?>```

Parse error: syntax error, unexpected T_STRING, expecting ')' in /home/mouncerc/public_html/fashizzlepop/challenge.php on line 21

spyware's Avatar
Banned
0 0

Check $values = array and use a syntax highlighting editor (like Notepad++ if using Windows).


ghost's Avatar
0 0

Sorry, I copied it from nano, the correct code is:

<?php
$values = array('0','1','2','3','4','5','6','7','8','9','a','A','b','B','c','C','d','D','e','E','f','F','g','G','h','H','i','I','j','J','k','K','l','L','m','M','n','N','o','O','p','P','q','Q','r','R','s','S','t','T','u','U','v','V','w','W','x','X','y','Y','z','Z');

$thetext = $_GET['hash'];

$newtext = wordwrap($thetext, 32, ",", true);
$hashes = explode(",", $newtext);

function modeish($n)
{

$m = md5($n);

return($m);
}

$hashed = array_map("modeish", $values);
foreach ($hashes as $a)
{
foreach ($hashed as $b)
{
if ($a == $b)
{
echo "$b";
}
}
}
?>

(The $ is because it went on farther than the width of the screen)


fashizzlepop's Avatar
Member
0 0

I copied that code exactly and nothing gets displayed… :(

I have no idea whats wrong. There isn't a error.


Mr_Cheese's Avatar
0 1

print_r your arrays, so we can actually see and try and help.

i reakon its a $name => $var problem like i mentioned above.


ghost's Avatar
0 0
<?php
$values = array('0','1','2','3','4','5','6','7','8','9','a','A','b','B','c','C','d','D','e','E','f','F','g','G','h','H','i','I','j','J','k','K','l','L','m','M','n','N','o','O','p','P','q','Q','r','R','s','S','t','T','u','U','v','V','w','W','x','X','y','Y','z','Z');

$thetext = $_GET['hash'];

$newtext = wordwrap($thetext, 32, ",", true);
$hashes = explode(",", $newtext);

function modeish($n)
{

$m = md5($n);

return($m);
}
echo '$hashes: ';
print_r($hashes);
echo "<br /><br />";
$hashed = array_map("modeish", $values);
echo '$hashed: ';
print_r($hashed);
echo "<br /><br />";
foreach ($hashes as $a)
{
foreach ($hashed as $b)
{
if ($a == $b)
{
echo "$b<br />";
}
}
}
?>```

That displays http://jonnycake.kicks-ass.net/test.php?hash=0cc175b9c0f1b6a831c399e26977266192eb5ffee6ae2fec3ad71c777531578f4a8a08f09d37b73795649038408b5f338277e0910d750195b448797616e091ade1671797c52e15f763380b45e841ec32

fashizzlepop's Avatar
Member
0 0

Ok hmm, I wonder why it wasn't working for me… but now I just realized I need to reformat it differently… Thanks for the help!!!