i am trying to implement in php a parser which reads binary files of various sizes (e.g. $fsize). In these files there are repeating 16-byte sequences wich i want to process independently (one 16-byte sequence at a time). I have managed to read and isolate each such 16-byte sequence in the form of:
processing sample[2]...
Printing 16 sample bytes
$bit_sequence='10000000010111001000000001100111011010110110011110100110101000100111011100111111000100000000000010100011010010101001101111101000'
Array:
$bits=( [0] => 1 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 1 [10] => 0 [11] => 1 [12] => 1 [13] => 1 [14] => 0 [15] => 0 [16] => 1 [17] => 0 [18] => 0 [19] => 0 [20] => 0 [21] => 0 [22] => 0 [23] => 0 [24] => 0 [25] => 1 [26] => 1 [27] => 0 [28] => 0 [29] => 1 [30] => 1 [31] => 1 [32] => 0 [33] => 1 [34] => 1 [35] => 0 [36] => 1 [37] => 0 [38] => 1 [39] => 1 [40] => 0 [41] => 1 [42] => 1 [43] => 0 [44] => 0 [45] => 1 [46] => 1 [47] => 1 [48] => 1 [49] => 0 [50] => 1 [51] => 0 [52] => 0 [53] => 1 [54] => 1 [55] => 0 [56] => 1 [57] => 0 [58] => 1 [59] => 0 [60] => 0 [61] => 0 [62] => 1 [63] => 0 [64] => 0 [65] => 1 [66] => 1 [67] => 1 [68] => 0 [69] => 1 [70] => 1 [71] => 1 [72] => 0 [73] => 0 [74] => 1 [75] => 1 [76] => 1 [77] => 1 [78] => 1 [79] => 1 [80] => 0 [81] => 0 [82] => 0 [83] => 1 [84] => 0 [85] => 0 [86] => 0 [87] => 0 [88] => 0 [89] => 0 [90] => 0 [91] => 0 [92] => 0 [93] => 0 [94] => 0 [95] => 0 [96] => 1 [97] => 0 [98] => 1 [99] => 0 [100] => 0 [101] => 0 [102] => 1 [103] => 1 [104] => 0 [105] => 1 [106] => 0 [107] => 0 [108] => 1 [109] => 0 [110] => 1 [111] => 0 [112] => 1 [113] => 0 [114] => 0 [115] => 1 [116] => 1 [117] => 0 [118] => 1 [119] => 1 [120] => 1 [121] => 1 [122] => 1 [123] => 0 [124] => 1 [125] => 0 [126] => 0 [127] => 0 )
I also have an array containing the variables to which i want to store the above information ...
Array:
$variables=( [0] => wide_avg_txon [1] => wide_avg [2] => wide_peak_rms [3] => wide_peak [4] => low_pw [5] => low_pp [6] => high_pw [7] => high_pp [8] => battery_voltage [9] => temperature_spr_flag [10] => temperature [11] => alarm_abat [12] => alarm_amem [13] => alarm_atmp [14] => alarm_alck [15] => alarm_prx [16] => alarm_arpb [17] => alarm_awrn [18] => alarm_aalr [19] => mask_mbat [20] => mask_mmem [21] => mask_mtmp [22] => mask_mlck [23] => mask_smx [24] => mask_mprb [25] => mask_mwrn [26] => mask_malr [27] => sample_rate [28] => avg_period [29] => months [30] => date_time )
My goal is to find a way to process the whole 128-bit sequence like an array with 128 elements and have the option to extract in such a way:
bits[0] -> store in variable $wide_avg_txon (boolean)
bits[1->15] -> store in variable $wide_avg (15-bit unsigned int)
bits[16] -> store in variable $wide_peak_rms (boolean)
bits[17->31] -> store in variable $wide_peak (15-bit unsigned int)
bits[32->39] -> store in variable $low_pw (8-bit unsigned int)
.... (and so on)
bits[112->127]-> store in variable $date_time (16-bit unsigned int)
i am quite new in php and in programming also...
I do not know if it is optimal solution ... but i have managed to store strings representing binary sequences of 1,8 or 16 bits to variables using code like this:
$wide_avg_txon = substr($bit_sequence,0,1);
//wide_avg_txon: 1
$wide_avg = '0'.substr($bit_sequence,1,15);
//wide_avg: 0000000001100000
$wide_peak_rms = substr($bit_sequence,16,1);
//wide_peak_rms: 1
$wide_peak = '0'.substr($bit_sequence,17,15);
//wide_peak: 0000000001100100
now i will need to process somehow these strings (representing binary numbers) numerically and convert to decimal numbers ...
Any suggestions to my posted solution would be useful and welcome...
In fact i managed to break the 128-bit sequence as desired using a quite dirty way:
$wide_avg = '0'.substr($bit_sequence,1,15);
$wide_avg_dec=bindec($wide_avg);
echo "wide_avg: " . $wide_avg . ", and as decimal: " . $wide_avg_dec . "";
and so on...
It seems you are just making an array from a string:
function getArrayFromString($string)
{
$length = strlen($string);
$ret = array();
for ($i = 0; $i < $length; $i++)
{
$ret[] = $string[$i];
}
}
However, it sounds like you are using the byte sequence like flags. This should be like what you need:
define("ADMIN_PRIV", 1); // 1<<1
define("DEV_PRIV", 4); // 1<<3
if ($byte & ADMIN_PRIV)
{
// this user is admin
}
however, PHP's integer is only 32-bits long (4 bytes), so that is the most that you can work with. So I would work on a per byte basis.
Related
I make a order-list of items whereby the stock is below minimum.
I make a array of al the items we have, filled with 0.
Now I want to enter the number of what must be ordered.
<td><INPUT TYPE="number" NAME="nordering[<php echo $store_central_id ?>]" SIZE="4" maxlength="4"></td>
I see only the last number what must be ordered and even by this not the id_store_central
I look in the database how many items i have, then i make an array for ($i=1;$i<$number;$i++) {$nordering[$i]=0; }
With print_r($nordering) i see only Array ( [0] => 012 ) while there are 317 items in the database.
After filling the array it is like this: Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15] => 0 [16] => 0 [17] => 0 [18] => 0 [19] => 0 [20] => 0 [21] => 0 [22] => 0 [23] => 0 [24] => 0 [25] => 0 [26] => 0 [27] => 0 [28] => 0 [29] => 0 [30] => 0 [31] => 0 [32] => 0 [33] => 0 [34] => 0 [35] => 0 [36] => 0 [37] => 0 [38] => 0 [39] => 0 [40] => 0 [41] => 0 [42] => 0 [43] => 0 [44] => 0 [45] => 0 [46] => 0 [47] => 0 [48] => 0 [49] => 0 etc
So my solution is now to make two arrays, one with the id's and one with the numbers like:
<INPUT TYPE="hidden" NAME="norder_items[]" VALUE="<?PHP echo store_central_id ?>">
<td><INPUT TYPE="number" NAME="nordering[]" SIZE="4" maxlength="4"></td>
Seems like you should do it like this:
NAME="<php echo $nordering[$store_central_id]?>"
Just needs more information about your code.
This question already has answers here:
array_search return wrong key [duplicate]
(4 answers)
Closed 4 years ago.
I have an large array. I need to find the needle of a specific text.
Therefore I use:
$afmeting = array_search('extmax', $lines);
When I echo $afmeting there is nothing there. But when I use print_r on the array I see it is there (needle 41). So what might be wrong?
Array ( [0] => 0 [1] => section [2] => 2 [3] => header [4] => 9 [5] => acadver [6] => 1 [7] => ac1027 [8] => 9 [9] => acadmaintver [10] => 70 [11] => 105 [12] => 9 [13] => dwgcodepage [14] => 3 [15] => ansi_1252 [16] => 9 [17] => requiredversions [18] => 160 [19] => 0 [20] => 9 [21] => lastsavedby [22] => 1 [23] => user [24] => 9 [25] => insbase [26] => 10 [27] => 0 [28] => 20 [29] => 0 [30] => 30 [31] => 0 [32] => 9 [33] => extmin [34] => 10 [35] => 0 [36] => 20 [37] => 0 [38] => 30 [39] => 0 [40] => 9 [41] => extmax [42] => 10 [43] => 260 [44] => 20 [45] => 150 [46] => 30 [47] => 0 [48] => 9 [49] => limmin [50] => 10 [51] => -26 [52] => 20 [53] => -15 [54] => 9 [55] => limmax [56] => 10 [57] => 286 [58] => 20 [59] => 165 [60] => 9 [61] => orthomode [62] => 70 [63] => 0 [64] => 9 [65] => regenmode [66] => 70 [67] => 1 [68] => 9 [69] => fillmode [70] => 70 [71] => 1 [72] => 9 [73] => qtextmode [74] => 70 [75] => 0 [76] => 9 [77] => mirrtext [78] => 70 [79] => 1 [80] => 9 [81] => ltscale [82] => 40 [83] => 1 [84] => 9 [85] => attmode [86] => 70 [87] => 1 [88] => 9 [89] => textsize [90] => 40 [91] => 0.2 [92] => 9 [93] => tracewid [94] => 40 [95] => 05 [96] => 9)
print_r gives: [41] => extmax
var_dump gives: [41]=> string(8) "extmax "
EDIT after alle tips and tricks:
Thank you all for contributing, so when I use this code all should be ok?
$afmeting = array_search('string(8) "extmax "', $lines, true);
But still no value when I echo $afmeting.
$array=array(0=>'0',1=>'section',3=>'header',4=>'extmax');
$afmeting = array_search('extmax', $array);
echo $afmeting;
The error is happening because of your "int" records in the array. If you mark all your array values as string, you will get your array key result. The above code will return you the array key 4. Instead if 0 was an int value it would not return the array key that your needle is located.
A solution to this is after you get your array you use array_map function to convert all your array field to string like below:
$array=array(0=>0,1=>'section',3=>'header',4=>'extmax');
$array = array_map('strval', $array);
$afmeting = array_search('extmax', $array);
echo $afmeting;
This will provide you with the right key.
Edit
Another solution is to use parameter $strict of array_search and set it to true
$array=array(0=>0,1=>'section',3=>'header',4=>'extmax');
$afmeting = array_search('extmax', $array, true);
echo $afmeting;
This will also provide you with the right array key.
More about array_search function you can find here!!!
I am using the following code to find the data from database
echo $query = "select * from `oc_order` where `store_url`='' ORDER BY `order_id` DESC LIMIT 5";
$mysqlq = mysqli_query($connection,$query) or die(mysqli_error($connection));
$mysqlw = mysqli_fetch_array($mysqlq);
echo "<pre>";
print_r($mysqlw);
foreach($mysqlw as $mysqlw)
{
echo $mysqlw['telephone'];
}
?>
I am getting the following array
Array
(
[0] => 73
[order_id] => 73
[1] => 0
[invoice_no] => 0
[2] =>
[invoice_prefix] =>
[3] => 0
[store_id] => 0
[4] =>
[store_name] =>
[5] =>
[store_url] =>
[6] => 9
[customer_id] => 9
[7] => 1
[customer_group_id] => 1
[8] => T****
[firstname] => ****
[9] =>
[lastname] =>
[10] => ********
[email] => *******
[11] => *****
[telephone] => *****
[12] =>
[fax] =>
[13] => 364ce6e2003bb1404f34
[custom_field] => 364ce6e2003bb1404f34
[14] =>
[payment_firstname] =>
[15] =>
[payment_lastname] =>
[16] =>
[payment_company] =>
[17] =>
[payment_address_1] =>
[18] =>
[payment_address_2] =>
[19] =>
[payment_city] =>
[20] =>
[payment_postcode] =>
[21] =>
[payment_country] =>
[22] => 0
[payment_country_id] => 0
[23] =>
[payment_zone] =>
[24] => 0
[payment_zone_id] => 0
[25] =>
[payment_address_format] =>
[26] =>
[payment_custom_field] =>
[27] =>
[payment_method] =>
[28] =>
[payment_code] =>
[29] =>
[shipping_firstname] =>
[30] =>
[shipping_lastname] =>
[31] =>
[shipping_company] =>
[32] =>
[shipping_address_1] =>
[33] =>
[shipping_address_2] =>
[34] =>
[shipping_city] =>
[35] =>
[shipping_postcode] =>
[36] =>
[shipping_country] =>
[37] => 0
[shipping_country_id] => 0
[38] =>
[shipping_zone] =>
[39] => 0
[shipping_zone_id] => 0
[40] =>
[shipping_address_format] =>
[41] =>
[shipping_custom_field] =>
[42] =>
[shipping_method] =>
[43] =>
[shipping_code] =>
[44] => 1,19
[comment] => 1,19
[45] => 100.0000
[total] => 100.0000
[46] => 0
[order_status_id] => 0
[47] => 0
[affiliate_id] => 0
[48] => 0.0000
[commission] => 0.0000
[49] => 0
[marketing_id] => 0
[50] =>
[tracking] =>
[51] => 0
[language_id] => 0
[52] => 0
[currency_id] => 0
[53] => INR
[currency_code] => INR
[54] => 1.00000000
[currency_value] => 1.00000000
[55] =>
[ip] =>
[56] =>
[forwarded_ip] =>
[57] =>
[user_agent] =>
[58] =>
[accept_language] =>
[59] => 0000-00-00 00:00:00
[date_added] => 0000-00-00 00:00:00
[60] => 0000-00-00 00:00:00
[date_modified] => 0000-00-00 00:00:00
)
but when i am trying to print the value of echo $mysqlw['telephone']; it is throwing warning Warning: Illegal string offset 'telephone' in
What mistake I am making in this code ?
You are trying to do a foreach on the array that you've displayed in your post. That means you're cycling through each item in the array. By trying to access $mysqlw['telephone'] you're expecting the item in the array to be another array containing the key 'telephone'.
I believe you're intending to do this:
while ($mysqlw = mysqli_fetch_array($mysqlq) {
echo $mysqlw['telephone'];
}
But also, you really should read up on how to do MySQL in a more modern style. Using things like mysqli_query in the manner you've used it here can open you up to injection attacks when you start having user input involved in the query.
Note your foreach statement has same variable name on both sides of 'as'
foreach($mysqlw as $mysqlw)
If you want to loop and access each value of an array do this instead:
foreach($mysqlw as $value)
{
echo $value;
}
Does somebody knows how I can refill this kind of array? I need to insert some values instead of the 0.
I make two loop, one for creating the array structure, and the another for filling the array, but when I want to file, I get an error "offset 0".
The values are integer numbers.
And I have a for loop:
$lista[$medio] = Array();
for ($count=1; $count<=53; $count++) {
$lista[$medio][$count] = 0;
}
for ($j = 1; $j <= $counter ; $j++) {
$f = $d[0]['f'];
$xn= $d[0]['d'];
$xww= explode("/",$xn);
$kw = $xww[0];
// Here I'm filling the list, I need the values from $f to be inserted instead of the 0 from the array. I think this is the correct way to do that:
$lista[$medio][intval($kw)]+= $f;
print_r($lista);
}
// But I get "offset 0" error
Array
(
[ge] => Array
(
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
[13] => 0
[14] => 0
[15] => 0
[16] => 0
[17] => 0
[18] => 0
[19] => 0
[20] => 0
[21] => 0
[22] => 0
[23] => 0
[24] => 0
[25] => 0
[26] => 0
[27] => 0
[28] => 0
[29] => 0
[30] => 0
[31] => 0
[32] => 0
[33] => 0
[34] => 0
[35] => 0
[36] => 0
[37] => 0
[38] => 0
[39] => 0
[40] => 0
[41] => 0
[42] => 0
[43] => 0
[44] => 0
[45] => 0
[46] => 0
[47] => 0
[48] => 0
[49] => 0
[50] => 0
[51] => 0
[52] => 0
[53] => 0
)
)
you can use PHP function array_fill_key
http://php.net/manual/de/function.array-fill-keys.php
http://php.net/manual/de/function.array-fill.php
Example from PHP:
<?php
$keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'Banane');
print_r($a);
?>
I hope works for you!
Hi I have an array that looks something like this
Array ( [0] => T [1] => T [2] => T [3] => T [4] => T [5] => T [6] => T [7] => T [8] => T [9] => T [10] => T [11] => T [12] => T [13] => T [14] => T [15] => T [16] => T [17] => T [18] => T [19] => T [20] => T [21] => T [22] => T [23] => T [24] => T [25] => T [26] => T [27] => T [28] => T [29] => T [30] => T [31] => T [32] => T [33] => T [34] => T [35] => T [36] => T [37] => T [38] => T [39] => T [40] => T [41] => T [42] => T [43] => T [44] => T [45] => T [46] => T [47] => T [48] => T [49] => T [50] => T [51] => T [52] => T [53] => T [54] => T [55] => T [56] => T [57] => T [58] => T [59] => T [60] => T [61] => T [62] => T [63] => T [64] => T [65] => T [66] => T [67] => T [68] => T [69] => T [70] => T [71] => T [72] => 0 [73] => 0 [74] => 0 [75] => 0 [76] => 0 [77] => 0 [78] => 0 [79] => 0 [80] => 0 [81] => 0 [82] => 0 [83] => 0 [84] => 0 [85] => 0 [86] => 0 [87] => 0 [88] => 0 [89] => 0 [90] => 0 [91] => 0 [92] => 0 [93] => 0 [94] => 0 [95] => 0 [96] => 0 [97] => 0 [98] => 0 [99] => 0 )
What I need to do is group keys [0]-[49], [50]-[99], and [100]-[149] into three string variables so tht they can be inserted into the database fields 'answ1-50', 'answ51-100', and 'answ101-150. Im a little bit lost on this one as I am still somewhat of a php novice. How do i break up this array into three groups as a string?
While not good practice, the answer to your posted question is to:
list($group1, $group2, $group3) = array_map("implode", array_chunk($arr, 50));
(assumes that you always have 150 answers. if you don't, skip the list and you will have an array of results $groups[0], $groups[1], $groups[2])
I'm not sure why you want to do this, but this should do the trick.
//assuming content in $myarray
$newarr = array();
for($i = 0; $i < count($myarray); $i++ {
$j = floor($i / 50);
if($j == $i / 50) {
$newarr[$j] = "";
}
$newarr[$j] += (string)$myarray[$i];
}
Each element of $newarr should be a string with 50 answers in it.
You can just simply write it under the loop and concatenate the values to a string.
for($i = 0; $i < count($yourarray); $i++) {
if(i>=0 && $i<50)
{
$str1=$str1.$yourarray[i];
}
else if(i>=50 && $i<100)
{
$str2=$str2.$yourarray[i];
}
else if(i>=100 && $i<150)
{
$str3=$str3.$yourarray[i];
}}