mercredi 8 décembre 2021

Create Unique random_bytes() for Each Element in an Array

There is a goal to add a unique ID to each of json responses. I'm intended to use random_bytes() PHP function. In my code I set up a variable:

$bytes = random_bytes(20);

Later I tried to add an unique identifier to each element in an array:

$bytes = random_bytes(20);
$json = file_get_contents($url, false, $context);
$result = json_decode($json, true);
foreach($result['surveys'] as $survey) {
    echo 'ssi=',bin2hex($bytes),' ',$survey['project_id'],'<br>';
}

I got a response with identical IDs instead:

ssi=d409af794aeebabb761dc0ede721a588104718fb 29711156
ssi=d409af794aeebabb761dc0ede721a588104718fb 29600921

Will you please make changes in my code so that it will show different "ssi" for each element in a massive instead of replicating $bytes into every line?




Aucun commentaire:

Enregistrer un commentaire