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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.