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!
Related
I want to distribute in array to get homogeneous distribution.
I created an array with array_fill, and set a variable $fill with a valor up to 20 to distribute in that array.
What I need to get as result - for example:
$fill = 5;
// returns
Array (
[0] => 0
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 0
[6] => 0
[7] => 1
[8] => 0
[9] => 0
[10] => 0
[11] => 1
[12] => 0
[13] => 0
[14] => 0
[15] => 1
[16] => 0
[17] => 0
[18] => 0
[19] => 1
)
$fill = 10;
// returns
Array (
[0] => 0
[1] => 1
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 0
[7] => 1
[8] => 0
[9] => 1
[10] => 0
[11] => 1
[12] => 0
[13] => 1
[14] => 0
[15] => 1
[16] => 0
[17] => 1
[18] => 0
[19] => 1
)
What should I do for, $fill = 6, 18 or even 19 ?
Edit 1:
The remainders I don't have preference to start or end, I need to fill the array with at least fair distribution, so when I foreach the array, I don't get a long sequence of 1 or 0.
I tried this
$fill = 5;
$arr = array_fill(0,20,0);
$gap = 20 / $fill;
foreach($arr as $k=>$v) {
if( is_int($k / $gap) ) {
$arr[$k] = 1;
}
}
print_r($arr);
but it doesn't work with 8,12 or 18
Edit 2:
Sorry for the complication, I just need to fill the array with all 5,10 or 18 '1's, don't necessary the same quantity for zeros, but at least this:
$fill = 18;
// returns
Array (
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 1
[5] => 0
[6] => 1
[7] => 1
[8] => 1
[9] => 1
[10] => 1
[11] => 1
[12] => 1
[13] => 1
[14] => 1
[15] => 0
[16] => 1
[17] => 1
[18] => 1
[19] => 1
)
You can do it like below :
<?php
function getArray($fill,$noOfItem){
$maxIndex = floor($noOfItem/$fill);
$rang = range(1,20);
$finalArray = [];
foreach($rang as $key => $val){
if($val % $maxIndex == 0 && array_sum($finalArray) < $fill){
$finalArray[] =1;
}else{
$finalArray[] =0;
}
}
print_r($finalArray);
}
getArray(6,20);
getArray(8,20);
getArray(19,20);
Output: https://3v4l.org/QY6HQ And https://3v4l.org/gQeHa (for a bit uniformity)
Note: Uniformity of 0 and 1 is not 100% guarantee in above answer.
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.
I have 2 array variable I want to sum array and divide into two parts. Please look my code -
print_r($public);
output -
Array
(
[0] => 2
[1] => 3
[2] => 5
[3] => 2
[4] => 1
[5] => 32
[6] => 5
[7] => 20
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
[13] => 0
[14] => 7
[15] => 0
[16] => 0
[17] => 0
[18] => 0
[19] => 0
[20] => 0
[21] => 0
[22] => 0
[23] => 0
[24] => 0
[25] => 0
[26] => 11
[27] => 0
[28] => 0
[29] => 0
[30] => 0
)
print_r($private);
Array
(
[0] => 0
[1] => 1
[2] => 0
[3] => 0
[4] => 1
[5] => 0
[6] => 0
[7] => 3
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
[13] => 0
[14] => 7
[15] => 0
[16] => 0
[17] => 0
[18] => 0
[19] => 0
[20] => 0
[21] => 0
[22] => 4
[23] => 0
[24] => 0
[25] => 0
[26] => 0
[27] => 0
[28] => 2
[29] => 0
[30] => 0
)
My Output should be -
$variable1=array_sum($public) + array_sum($private); //for First 15 days(array)
$variable2 = array_sum($public) + array_sum($private); //For 16 to end of the array
$public and $private is two array. contans May month date wise records. I want to sum array value into two divide parts.
1st - Day - 1-15(sum of array 0 to 14)
2nd - Day - 16-end of month(sum of array 15 to end of the array)
How to calculate both variable into two divide parts in a single line code?
You can use array_slice:
$first_part = array_sum(array_slice($public, 0, 15));
$second_part = array_sum(array_slice($public, 15, 16));
$first_part = array_sum(array_slice($public, 0, 15)) + array_sum(array_slice($private, 0, 15));
$second_part = array_sum(array_slice($public, 15, 16)) + array_sum(array_slice($private, 15, 16));
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];
}}
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.