3

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 ?

Damir
  • 51,611
  • 92
  • 240
  • 358

3 Answers3

2
$myarray = array(1,2,16,29,33,46,69);

is this an array of byte?

thaolt
  • 475
  • 2
  • 7
2

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,501
  • 2
  • 26
  • 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
  • 2,945
  • 5
  • 24
  • 45