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.
Related
This is my default array and this concat_group will explode in my code
MySQL CODE
this is my query to get data
SELECT
results.StuID,
subjects_d.SubjectID,
subjects.ID,
users.*,
exams.ID,
classes.Name AS ClName,
GROUP_CONCAT(subjects.Super_Degree, ',',subjects_d.SubjectID, ',', results.ExamID, ',',results.Exam1,',',results.Exam2,',',results.Exam3 ORDER BY subjects_d.SubjectID, results.ExamID ASC) AS data
FROM
results
LEFT JOIN
users
ON
users.UID = results.StuID
LEFT JOIN
subjects_d
ON
subjects_d.SubID = results.Sub_ID
LEFT JOIN
subjects
ON
subjects_d.SubjectID = subjects.ID
LEFT JOIN
exams
ON
exams.ID = results.ExamID
INNER JOIN
classes
ON
classes.CID = subjects_d.C_ID
WHERE
subjects_d.C_ID = ?
GROUP BY results.StuID
ORDER BY StuID
PHP CODE
here I want to explode my array from MySQL
<?php foreach ($arr as $student) {
<?php $data_array = explode(',',$student['data']); ?>
<?php echo '<pre>'; print_r($data_array) ?>
<?php } ?>
OUTPUT
the output from print_r.
Array
(
[0] => 1
[1] => 90
[2] => 0
[3] => 0
[4] => 0
[5] => 2
[6] => 90
[7] => 0
[8] => 0
[9] => 0
[10] => 3
[11] => 90
[12] => 0
[13] => 0
[14] => 0
}
I need to Explode this to multi-array like it
Need to get this output
Array[0]
(
[0] => 1
[1] => 90
[2] => 0
[3] => 0
[4] => 0
)
Array[1]
(
[5] => 2
[6] => 90
[7] => 0
[8] => 0
[9] => 0
)
Array[2]
(
[10] => 3
[11] => 90
[12] => 0
[13] => 0
[14] => 0
)
need your help to get this output arrays
Thank you.
You can use array_chunk for this.
$final = array_chunk( $arr, 5, true );
This will result to:
Array
(
[0] => Array
(
[0] => 1
[1] => 90
[2] => 0
[3] => 0
[4] => 0
)
[1] => Array
(
[5] => 2
[6] => 90
[7] => 0
[8] => 0
[9] => 0
)
[2] => Array
(
[10] => 3
[11] => 90
[12] => 0
[13] => 0
[14] => 0
)
)
Documentation: http://php.net/manual/en/function.array-chunk.php
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));
In the controller
My Matrix is $mes:
print_r($mes);
Array (
[0] => Array (
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 3
[7] => 1
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
)
)
Array (
[1] => Array (
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 2
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
)
)
Send data to view:
$this->view->repMes = $mes;
In the view
<?php print_r($this->repMes); ?>
Array (
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 2
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
)
As can be evidenced missing the first part of the matrix, as it could be solved?
I believe you want to have a two dimensional array but looking at you code i see you have two different arrays . so you $mes variable points to the second array that is why you see only the second array in the view. you should have you array in the controller as:
$mes = Array (
[0] => Array (
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 3
[7] => 1
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
)
[1] => Array (
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 2
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
)
)
Notice when you print your matrix, it shows as two different arrays, that is because your code flow is not sending the right array.
Your array "$mes" should be this:
Array (
[0] => Array (
[0] => 0,
[1] => 0,
[2] => 0,
[3] => 0,
[4] => 0,
[5] => 0,
[6] => 3,
[7] => 1,
[8] => 0,
[9] => 0,
[10] => 0,
[11] => 0,
[12] => 0 ),
[1] => Array ( // See here you should have the second subindex, and not the closing and reopening
[0] => 0, // of "Array"
[1] => 0,
[2] => 0,
[3] => 0,
[4] => 0,
[5] => 0,
[6] => 2,
[7] => 0,
[8] => 0,
[9] => 0,
[10] => 0,
[11] => 0,
[12] => 0)
)
I would suggest using a pro-tempore counter variable to check if your "print" is repeating
Some like:
echo "<pre> Loop --> \n";
print_r($mes);
echo "End Loop\n</pre>";
This way you can check the real array.
I have an associative array in php.
the content of associative array looks like this:
Array
(
[0] => Array
(
[0] => 3
[1] => 1
[2] => 0
[3] => 50074494
[4] => 25013372
[5] => 2
[6] => 474
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 985
[12] => 34951
[13] => 18143
[14] => 4
[15] => 2
[16] => 94
[17] => 1
[18] => 1.26
[19] => 7.9
[20] => 2013-06-27 10:19:21
)
[1] => Array
(
[0] => 5
[1] => 1
[2] => 0
[3] => 50078122
[4] => 25000164
[5] => 2
[6] => 463
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 860
[12] => 28290
[13] => 16944
[14] => 4
[15] => 1
[16] => 94
[17] => 1
[18] => 1.13
[19] => 7.1
[20] => 2013-06-27 10:19:51
)
[2] => Array
(
[0] => 4
[1] => 1
[2] => 0
[3] => 50078630
[4] => 24995538
[5] => 2
[6] => 155
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 616
[12] => 23203
[13] => 4892
[14] => 3
[15] => 1
[16] => 95
[17] => 0
[18] => 1.04
[19] => 6.5
[20] => 2013-06-27 10:20:21
)
)
I would like to be able to assign the inner array values to a variable. I need variable to look like this:
echo $variable
3 1 0 50074494 25013372 2 474 .. 2013-06-27 10:19:21
.
.
I have this code so far:
$variable;
foreach ($lines as $key => $value) {
foreach ($value as &$val)
{
$variable=$variable . $val . ' ';
}
echo $variable;
echo "\n";
}
with this code it looks like I am getting 3 times of variable. Any ideas what I am doing wrong here?
If you have an array, and you want to store the values in a space-separated string, you could do this:
$string = implode(' ', $array);
echo $string;
So your loop might look like this:
foreach ($lines as $value) {
$value[20] = '"'. $value[20] .'"'; // from comments
echo implode(' ', $value) ."\n";
}
I would recommend the use of implode instead of foreach. Also, it doesn't seem like you need the value of $key:
foreach ($lines as $value) {
echo implode(" ", $value);
echo "\n";
}
Also, I'm not sure I quite understood your question? What do you mean by "3 times of variable"?
I have an array that represents UNIX time in int type from a table I can't change. Some of the rows are not full UNIX timestamps but are short by a couple of integers. There is a reason this is so in the table but for my script, I need the string to change the non 10 digit rows into "0" and the 10 digit ones into date("Ymd",?) form. Here's an example of the array $qucls:
Array
(
[0] => 1332594303
[1] => 1330960502
[2] => 1331227649
[3] => 1331305503
[4] => 1332594303
[5] => 1331147102
[6] => 1332680702
[7] => 1331301902
[8] => 1331048163
[9] => 1332248704
[10] => 1332421503
[11] => 31536000
[12] => 1331816703
[13] => 604800
[14] => 0
[15] => 31536000
[16] => 1332248703
[17] => 31536000
[18] => 1361922903
)
This is the script:
$k=0
$l=0
foreach ($qucls as $dt[$k]){
if (strlen($dt[$k]) < 10)
$dt[$k++] = '0';
else {$dt[$k++] = date("Ymd", $dt[$l++]);
}
}
for ($l=0; $l < $k; $l++){
}
This is the outcome after the loop:
Array
(
[0] => 20120324
[1] => 20120305
[2] => 20120308
[3] => 20120309
[4] => 20120324
[5] => 20120307
[6] => 20120325
[7] => 20120309
[8] => 20120306
[9] => 20120320
[10] => 20120322
[11] => 0
[12] => 19700101
[13] => 0
[14] => 0
[15] => 0
[16] => 19700817
[17] => 0
[18] => 19700101
)
Notice the date form is formatted properly until it reaches the 1st integer that is strlen < 10. At that point, it changes the less than 10 length integer to "0" which is correct, but the dates after that are goofed up. It continues to change the < 10 digit ones to 0 correctly.
Can someone help me figure out what is wrong in this loop? I'm not quite getting the right outcome with all those 1970 dates after the ELSE kicks in. I'm still new at this.
Thank you.
Use the below script
<?php
$qucls = array(
0 => 1332594303,
1 => 1330960502,
2 => 1331227649,
3 => 1331305503,
4 => 1332594303,
5 => 1331147102,
6=> 1332680702,
7=> 1331301902,
8=> 1331048163,
9=> 1332248704,
10 => 1332421503,
11 => 31536000,
12 => 1331816703,
13 => 604800,
14 => 0,
15 => 31536000,
16 => 1332248703,
17 => 31536000,
18 => 1361922903
);
foreach ($qucls as $key=>$value){
if (strlen($value)< 10){
$dt[] = 0;
}else{
$dt[] = date("Ymd", $value);
}
}
echo "<pre>";
print_r($array);
print_r($dt);
exit;
?>
and you will get the below output
Array
(
[0] => 20120324
[1] => 20120305
[2] => 20120308
[3] => 20120309
[4] => 20120324
[5] => 20120307
[6] => 20120325
[7] => 20120309
[8] => 20120306
[9] => 20120320
[10] => 20120322
[11] => 0
[12] => 20120315
[13] => 0
[14] => 0
[15] => 0
[16] => 20120320
[17] => 0
[18] => 20130226
)