Jump to content

Using arrays and sha1


megglz

Recommended Posts

I have the following array which builds a concatenation of the items in tree-like form.

 

 

$arrayT = array();
$arrayT[0] = "a";
$arrayT[1] = "b";
$arrayT[2] = "c";
$arrayT[3] = "d";
$arrayT[4] = "e";
$arrayT[5] = "f";
$arrayT[6] = "g";
$arrayT[7] = "h";

$arrayT = buildTree($arrayT);

print_r($arrayT);

function buildTree($array) {
    $arrayTree = $array;
    $start = 0;
    $end = count($arrayTree);
$i = 0;
    while ($start != $end - 1) {
        if ($i % 2 == 1) {
            $arrayTree[count($arrayTree) - 1].=$arrayTree[$start + $i];
        }
        else
            $arrayTree[] .= $arrayTree[$start + $i];
        $i++;
        if (($start + $i) == $end) {
            $i = 0;
            $start = $end;
            $end = count($arrayTree);
        }
    }
    return $arrayTree;
}

 

Example output is

 

Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => g [7] => h [8] => ab [9] => cd [10] => ef [11] => gh [12] => abcd [13] => efgh [14] => abcdefgh )

 

 

I would like the values to be hashed using sha1. e.g value at [8] ab would be the hash of [0]a and [1] b, value [12]abcd would be the hash of the values at [8]ab and [9]cd. I modified it myself in the next code snippet but I still don't feel it's doing what it's meant

 

function buildTree($array) {
    $arrayTree = $array;
    $start = 0;
    $end = count($arrayTree);
$i = 0;
    while ($start != $end - 1) {
        if ($i % 2 == 1) {
            $arrayTree[count($arrayTree) - 1].=$arrayTree[$start + $i];
		sha1($arrayTree[count($arrayTree)-1].=$arrayTree[$start + $i]);
        }
        else
            $arrayTree[] .= sha1($arrayTree[$start + $i]);
        $i++;
        if (($start + $i) == $end) {
            $i = 0;
            $start = $end;
            $end = count($arrayTree);
        }
    }
    return $arrayTree;
}

$val1 = sha1($arrayT[0]);

$val2 = sha1($arrayT[1]);

$val3 = sha1($val1.$val2);

$val4 = sha1("ab");
echo "VAL1 ".$val1;
echo "<br/>";
echo "VAL2 ".$val2;
echo "<br/>";
echo "VAL3 ".$val3;
echo "<br/>";
echo "VAL4 ".$val4;

 

 

Anyone any ideas? Thankyou.

Link to comment
https://forums.phpfreaks.com/topic/228405-using-arrays-and-sha1/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.