php adding values in same foreach - php
I am trying to add up all the books sold quantities, for example Book A sold 55, Book B sold 9, Book A 65, Book B sold 20. Book A = 120 and Book B = 29.
And as you can see i need to add up the numeral values per book. I just not sure how to add up values.
My array looks like this:
Array
(
[Visual Flight Guide] => Array
(
[0] => 1
[30] => 5
[39] => 3
[69] => 6
[118] => 10
)
[Flight Radio for Pilots VFR Operations] => Array
(
[1] => 1
[5] => 1
[17] => 5
[46] => 1
[53] => 5
[120] => 4
)
[ATC Pilots Logbook] => Array
(
[2] => 1
[3] => 1
[20] => 30
[27] => 26
[28] => 1
[40] => 8
[54] => 52
[66] => 30
[78] => 52
[121] => 30
)
[Aerobatics Principles and Practice] => Array
(
[4] => 1
)
[Instrument Flight Guide] => Array
(
[6] => 5
[18] => 1
[32] => 1
[41] => 6
[56] => 5
[70] => 6
[108] => 8
[130] => 5
)
[Command Instrument Rating] => Array
(
[7] => 10
[16] => 1
[35] => 1
[57] => 5
[67] => 6
[93] => 3
[113] => 2
)
[Student Pilot Kit] => Array
(
[8] => 5
[13] => 4
[44] => 1
[51] => 9
[61] => 12
[80] => 1
[102] => 5
[117] => 2
[134] => 1
)
[Basic Aeronautical Knowledge] => Array
(
[9] => 10
[29] => 5
[52] => 5
[63] => 6
[79] => 20
[94] => 1
[106] => 1
[110] => 12
[119] => 4
)
[Human Factors] => Array
(
[10] => 10
[22] => 1
[45] => 1
[83] => 6
[122] => 3
)
[ATC Rotating Plotter] => Array
(
[11] => 4
)
[ATC Protractor] => Array
(
[12] => 5
[42] => 12
[60] => 10
[74] => 30
[81] => 25
)
[180NM Scale Rule] => Array
(
[14] => 4
[43] => 12
[73] => 30
[82] => 25
)
[Seven Volume PPLCPL Kit] => Array
(
[15] => 4
[19] => 1
[31] => 1
[62] => 6
[77] => 1
[100] => 3
[128] => 3
)
[Fake Item] => Array
(
[21] => 1
)
[Flight Rules and Air Law] => Array
(
[23] => 1
[87] => 7
[97] => 1
[127] => 5
)
[Aircraft Operation Performance and Planning] => Array
(
[24] => 1
[88] => 4
[95] => 1
[112] => 6
[133] => 1
)
[Aerodynamics] => Array
(
[25] => 1
[91] => 1
[101] => 1
[104] => 1
[124] => 3
)
[Aircraft General Knowledge] => Array
(
[26] => 1
[84] => 5
[92] => 1
[99] => 1
[105] => 1
[123] => 3
)
[Human Being Pilot] => Array
(
[33] => 1
[68] => 3
[109] => 16
)
[Flight Rules and Air Law for the Air Transport Pilot] => Array
(
[34] => 1
[55] => 3
[65] => 3
[89] => 1
[129] => 2
)
[Aerodynamics Engines Airframe Systems for the Air Transport Pilot] => Array
(
[36] => 1
)
[Aeroplane Performance Planning Loading for the Air Transport Pilot] => Array
(
[37] => 1
[98] => 1
)
[Avionics and Flight Management Systems for the Air Transport Pilot] => Array
(
[38] => 1
)
[The Flying Training Manual] => Array
(
[47] => 1
[48] => 1
[103] => 6
[107] => 10
[116] => 2
)
[In Safe Hands] => Array
(
[49] => 3
)
[Helicopter Trial Instructional Flight] => Array
(
[50] => 3
)
[Flight at Lower Levels Safety in the Circuit] => Array
(
[58] => 3
)
[Night Flight] => Array
(
[59] => 3
[75] => 4
[114] => 1
[132] => 1
)
[Meteorology] => Array
(
[64] => 3
[85] => 7
[111] => 6
[125] => 3
)
[80NM Scale Rule] => Array
(
[71] => 10
)
[120NM Scale Rule] => Array
(
[72] => 10
)
[MultiEngine Piston] => Array
(
[76] => 2
)
[Navigation] => Array
(
[86] => 5
[96] => 2
[126] => 3
)
[Three Points Flying a Tailwheel Aircraft] => Array
(
[90] => 1
)
[Pilots Index] => Array
(
[115] => 1
[131] => 5
)
)
I have tried this:
$stock_taken = array();
$i = 0;
foreach($stock_take as $stock){
$stock_taken[$stock->name][$i] = $stock->qty;
$i++;
}
You have to use array_sum() for sum of values in array.so try this
//$array = your array
$sum = array();
foreach($array as $key=>$stock)
{
$sum[$key]=array_sum($stock);
}
foreach($sum as $key=>$values)
{
echo "sum of book ".$key." sold is ".$values."\n";
}
Demo
Related
Find key in array is not finding the text while it is there [duplicate]
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!!!
Remove consecutive occurrences of string "start" from array PHP ARRAY
I have the following array Array ( [0] => **start** [1] => **start** [2] => name [3] => producer [4] => contact name [5] => 03354222271 [6] => fzahid001#gmail.com [7] => day contact name [8] => 03354222271 [9] => venue name [10] => adress [11] => country [12] => city [13] => desc [14] => file [15] => 2016-01-01 [16] => 01:00 [17] => 06:00 [18] => 2 [19] => music_festival [20] => 2000+ [21] => quick [22] => alcohol [23] => quick [24] => 10x10 [25] => ***no*** [26] => ***no*** [27] => ***no*** [28] => 2 [29] => 0 [30] => 4 [31] => $158 [32] => $118.5 [33] => $284 [34] => $960 [35] => **start** [36] => na [37] => producer [38] => con [39] => 1 [40] => fzahid001#gmail.com [41] => nam [42] => 1 [43] => venue [44] => ad [45] => co [46] => ci [47] => description test [48] => download555ssss.png [49] => 2016-12-07 [50] => 13:00 [51] => 19:00 [52] => 2 [53] => manual_selection [54] => ATV [55] => 10x10 [56] => no [57] => no [58] => no [59] => 1 [60] => 1 [61] => 1 [62] => $109.5 [63] => $118.5 [64] => $99.5 [65] => $728 [66] => **start** [67] => Race [68] => Race Club [69] => Faizan [70] => 03354222271 [71] => fzahid001#gmail.com [72] => Faizan Zahid [73] => 03354222271 [74] => DHA [75] => 90-H Tariq Gardens [76] => Pakistan [77] => Lahore [78] => [79] => images.jpg [80] => 2017-01-01 [81] => 00:00 [82] => 07:00 [83] => 2 [84] => cycling_road_race [85] => 1_field [86] => 1_399 [87] => electronic [88] => quick [89] => 10x10 [90] => no [91] => no [92] => no [93] => 1 [94] => 0 [95] => 1 [96] => $109.5 [97] => $118.5 [98] => $99.5 [99] => $796.5 [100] => ) Now what I want to do is, remove the consecutive duplicates of "start". I am currently using following code foreach ($updateddata as $value){ if($value != $previousvalue){ array_push($finaldata, $value); } $previousvalue = $value; } but is also removes other consecutive duplicates as well which I don't want to remove. Kindly help me how to do this. I have highlighted the occurrences of "start" which I want to remove and make it one single entry, while leaving the other string as it is e.g. I don't want to remove the duplicates of string "no"
Try this if($value != $previousvalue){ array_push($finaldata, $value); } else if( $value != 'start'){ array_push($finaldata, $value); }
To remove the consecutive duplicates of "start" value you should restrict the crucial condition to that value. Use the following approach: // exemplary array $updateddata = [ 'start', 'start', 'producer', 'contact', 'no', 'no' ]; foreach ($updateddata as $k => $v) { if (isset($updateddata[$k-1]) && $v == 'start' && $v == $updateddata[$k-1]) { unset($updateddata[$k]); } } The output: Array ( [0] => start [2] => producer [3] => contact [4] => no [5] => no )
Wrong result for array_count_values in PHP
I have an array like this - Array ( [0] => একটু [1] => আরো [2] => নয় [3] => কাছে [4] => থাকো [5] => না [6] => কাছে [7] => ডাকো [8] => না [9] => আরো [10] => কাছে [11] => আজ [12] => দুজনের [13] => প্রথম [14] => মিলনের [15] => চাঁদ [16] => তারা [17] => ঐ [18] => সাক্ষী [19] => আছে [20] => তুমি [21] => আমার [22] => আমি [23] => তোমার [24] => [25] => মন [26] => যে [27] => ভরেছে [28] => জোছনা [29] => ঝরেছে [30] => পৃথিবী [31] => দ্যাখো [32] => ঐ [33] => ঘুমিয়ে [34] => পড়েছে [35] => মন [36] => যে [37] => ভরেছে [38] => জোছনা [39] => ঝরেছে [40] => পৃথিবী [41] => দ্যাখো [42] => ঐ [43] => ঘুমিয়ে [44] => পড়েছে [45] => আমি [46] => তোমার [47] => তুমি [48] => আমার [49] => একটু [50] => আরো [51] => নয় [52] => কাছে [53] => থাকো [54] => না [55] => কাছে [56] => ডাকো [57] => না [58] => আরো [59] => কাছে [60] => আমি [61] => তোমার [62] => তুমি [63] => আমার [64] => [65] => ফুলেরই [66] => হাসিতে [67] => হাওয়ার [68] => বাঁশিতে [69] => এ [70] => রাত [71] => শেখালো [72] => ভালো [73] => যে [74] => বাসিতে [75] => আমি [76] => তোমার [77] => তুমি [78] => আমার [79] => একটু [80] => আরো [81] => নয় [82] => কাছে [83] => থাকো [84] => না [85] => কাছে [86] => ডাকো [87] => না [88] => আরো [89] => কাছে [90] => তুমি [91] => আমার [92] => আমি [93] => তোমার [94] => ) When i am counting the same values using the following code it is giving wrong results like below - $wordCountArr = array_count_values($matchWords); Array ( [একটু] => 1 [আরো] => 6 [নয়] => 3 [কাছে] => 6 [থাকো] => 3 [না] => 6 [ কাছে] => 3 [ডাকো] => 3 [ আজ] => 1 [দুজনের] => 1 [প্রথম] => 1 [মিলনের] => 1 [ চাঁদ] => 1 [তারা] => 1 [ঐ] => 3 [সাক্ষী] => 1 [আছে] => 1 [ তুমি] => 2 [আমার] => 5 [আমি] => 2 [তোমার] => 5 [] => 3 [ মন] => 1 [যে] => 3 [ভরেছে] => 2 [জোছনা] => 2 [ঝরেছে] => 2 [ পৃথিবী] => 2 [দ্যাখো] => 2 [ঘুমিয়ে] => 2 [পড়েছে] => 2 [ মন] => 1 [ আমি] => 3 [তুমি] => 3 [ একটু] => 2 [ ফুলেরই] => 1 [হাসিতে] => 1 [হাওয়ার] => 1 [বাঁশিতে] => 1 [ এ] => 1 [রাত] => 1 [শেখালো] => 1 [ভালো] => 1 [বাসিতে] => 1 ) But why [কাছে] => 6 and [ কাছে] => 3 ??? It suppose to be [কাছে] => 9 Now should i trim the spaces before and after or match words with spaces ? I used array_map('trim',$matchWords) but no luck! How can i fix this? Please HELP !
Yes the matching is separate for those because of the space. trim() to remove spaces for each word wouldn't work for your case as it doesn't remove unicode white spaces. So your solution use: preg_replace. Refer: Trim unicode whitespace why is php trim is not really remove all whitespace and line breaks?
removing duplicate values in multi dimensional array
I have a multi dim. array with duplicate values. I need it to show only one single value of each in a dropdown, which in turn are used for a search Query. The indexs are not important here. I tried with array_map, serialize, array_unique and so on, but can't get it right. Some values allways comes out twice. I'm on php version 4.3.2, so can't use functions made after that version. The array looks like this: Array ( [1] => Array ( [1] => 0,9 [2] => 1,3 [3] => 1,6 [4] => 1,5 [5] => 0,0 [6] => 1,2 ) [2] => Array ( [3] => 1,0 [4] => 2,0 [5] => 1,78 [6] => 1,44 [7] => 1,87 [8] => 1,56 [9] => 2,4 [10] => 2,93 [11] => 2,50 [12] => 1,4 [13] => 6,0 [14] => 4,0 [15] => 1,80 [16] => 3,00 ) [3] => Array ( [5] => 1,0 [6] => 1,2 [7] => 1,5 [8] => 1,6 ) [4] => Array ( [7] => 1,0 ) [5] => Array ( [1] => 1,0 [2] => 1,5 [3] => 2,0 [4] => 2,5 [5] => 3,0 [6] => 3,5 [7] => 4,0 [8] => 4,5 ) [6] => Array ( [1] => 1,5 [2] => 2,0 [3] => 2,5 [4] => 3,0 [5] => 3,5 [6] => 4,0 [7] => 4,5 [8] => 5,0 [9] => 5,5 [10] => 6,0 [11] => 6,5 [12] => 7,0 [13] => 7,5 [14] => 8,0 [15] => 8,5 [16] => 9,0 [17] => 9,5 [18] => 10,0 ) [7] => Array ( [15] => 0,65 [16] => 0,75 [17] => 0,85 [18] => 0,90 [19] => 1,00 [20] => 1,10 [21] => 1,15 [22] => 1,25 [23] => 1,50 [24] => 1,55 [25] => 1,60 [26] => 1,80 [27] => 1,85 [28] => 2,00 [29] => 2,10 ) [8] => Array ( [30] => 0,90 [31] => 1,00 ) [10] => Array ( [34] => 0,65 [35] => 0,75 [36] => 0,85 [37] => 0,90 [38] => 1,00 [39] => 1,10 [40] => 1,15 [41] => 1,25 [42] => 1,50 [43] => 1,55 [44] => 1,60 [45] => 1,80 [46] => 1,85 [47] => 2,00 [48] => 2,10 ) [11] => Array ( [34] => 1,0 [35] => 1,5 [36] => 2,0 [37] => 2,5 [38] => 3,0 [39] => 3,5 [40] => 4,0 [41] => 4,5 ) [12] => Array ( [43] => 1,00 ) [13] => Array ( [45] => 2,00 [46] => 2,20 [47] => 2,50 [48] => 2,75 [49] => 3,00 [50] => 3,25 [51] => 3,50 [52] => 2,60 [53] => 2,85 [54] => 1,50 [55] => 2,10 [56] => 2,35 [57] => 2,80 [58] => 2,40 [59] => 2,70 [60] => 2,25 [61] => 3,40 [62] => 2,55 [63] => 2,30 [64] => 2,65 ) ) Any help would be appreciated.
To make each sub-array unique compared to other sub-arrays (the order is important): $array = array_map('unserialize', array_unique(array_map('serialize', $array))); To make only values within each sub-array unique: $array = array_map('array_unique', $array);
$array = array_map('unserialize', array_unique(array_map('serialize', $array))); is exactly what I tried, and from that same thread, but it comes out like this: Array( [1] => Array ( [1] => 0,9 [2] => 1,3 [3] => 1,6 [4] => 1,5 [5] => 0,0 [6] => 1,2 ) [2] => Array ( [3] => 1,0 [4] => 2,0 [5] => 1,78 [6] => 1,44 [7] => 1,87 [8] => 1,56 [9] => 2,4 [10] => 2,93 [11] => 2,50 [12] => 1,4 [13] => 6,0 [14] => 4,0 [15] => 1,80 [16] => 3,00 ) [3] => Array ( [5] => 1,0 [6] => 1,2 [7] => 1,5 [8] => 1,6 ) [4] => Array ( [7] => 1,0 ) [5] => Array ( [1] => 1,0 [2] => 1,5 [3] => 2,0 [4] => 2,5 [5] => 3,0 [6] => 3,5 [7] => 4,0 [8] => 4,5 ) [6] => Array ( [1] => 1,5 [2] => 2,0 [3] => 2,5 [4] => 3,0 [5] => 3,5 [6] => 4,0 [7] => 4,5 [8] => 5,0 [9] => 5,5 [10] => 6,0 [11] => 6,5 [12] => 7,0 [13] => 7,5 [14] => 8,0 [15] => 8,5 [16] => 9,0 [17] => 9,5 [18] => 10,0 ) [7] => Array ( [15] => 0,65 [16] => 0,75 [17] => 0,85 [18] => 0,90 [19] => 1,00 [20] => 1,10 [21] => 1,15 [22] => 1,25 [23] => 1,50 [24] => 1,55 [25] => 1,60 [26] => 1,80 [27] => 1,85 [28] => 2,00 [29] => 2,10 ) [8] => Array ( [30] => 0,90 [31] => 1,00 ) [10] => Array ( [34] => 0,65 [35] => 0,75 [36] => 0,85 [37] => 0,90 [38] => 1,00 [39] => 1,10 [40] => 1,15 [41] => 1,25 [42] => 1,50 [43] => 1,55 [44] => 1,60 [45] => 1,80 [46] => 1,85 [47] => 2,00 [48] => 2,10 ) [11] => Array ( [34] => 1,0 [35] => 1,5 [36] => 2,0 [37] => 2,5 [38] => 3,0 [39] => 3,5 [40] => 4,0 [41] => 4,5 ) [12] => Array ( [43] => 1,00 ) [13] => Array ( [45] => 2,00 [46] => 2,20 [47] => 2,50 [48] => 2,75 [49] => 3,00 [50] => 3,25 [51] => 3,50 [52] => 2,60 [53] => 2,85 [54] => 1,50 [55] => 2,10 [56] => 2,35 [57] => 2,80 [58] => 2,40 [59] => 2,70 [60] => 2,25 [61] => 3,40 [62] => 2,55 [63] => 2,30 [64] => 2,65 ) ) And I still got multiple values like 1,0
break 128 bit sequence to bits
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.