I need help with PHP, I need to define byte array and to change values of some bytes ( for example set 3rd byte to 16 or 17 and so on ). How to define array of bytes in PHP ?
Asked
Active
Viewed 1.1k times
3 Answers
3
I am not entirely sure what you mean when you say byte. But try this:
<?php
$bytes = array(1, 50, 39, 21, 93, 20);
$bytes[2] = 16; // Changes 3rd byte to 16

eisberg
- 3,731
- 2
- 27
- 38
2
You can define array easily like this:
$bytes = array(1,10,6,67);
change third element:
$bytes[2] = 5;
But be careful! If you delete element 1 (which is 10 in above example):
unset($bytes[1]);
the array will look like this:
array(1,5,67);
however 5 is still element at index 2
echo $bytes[0]; // this will output 1
echo $bytes[2]; // this will output 5
So to change the third element now you have to do this:
$bytes[3] = 123; // because array keys don't change and the third element is now $bytes[3] and not $bytes[2]

NickSoft
- 3,215
- 5
- 27
- 48