Matrix of Controller to View - ZF - php

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.

Related

Simple number distribution in array php

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.

Explode array to multi array

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

Php array remove duplicate and put toghether in another array

I have this php array:
Array
(
[0] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 0
[9] => 1
[10] => 1
)
[1] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 1
[9] => 0
[10] => 0
)
[2] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 0
[9] => 0
[10] => 0
)
[3] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 1
[9] => 0
[10] => 0
)
[4] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 0
[9] => 1
[10] => 0
)
)
I want i new array:
1) if element [0] exists in the array I want to control elements 8, 9 and 10 and if one of this is 1 I want to have 1 in the final array, but I don't want to have the same array 2 times (the index [0] is the key).
My final array shound be:
Array
(
[0] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 1
[9] => 1
[10] => 1
)
[3] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 1
[9] => 1
[10] => 0
)
)
I have only one time the index 2505 and 2505 and index 8 and 9 and 10 are 1 for the 2505 and the index 8 and 9 are 1 for the 2525.
this does the job: (assuming that your first array is $arrays and your desired result is $result)
$result = array();
foreach($arrays as $array)
{
if(!isset($result[$array[0]]))
{
$result[$array[0]] = $array;
}
else
{
$result[$array[0]][8] = $array[8] == 1 ? 1 : $result[$array[0]][8];
$result[$array[0]][9] = $array[9] == 1 ? 1 : $result[$array[0]][9];
$result[$array[0]][10] = $array[10] == 1 ? 1 : $result[$array[0]][10];
}
}
but wherever you get your data from, e.g. a database, I would use element[0] as the key, so you don't get multiple entries in $arrays. Another option would be to create a class which holds your data...

Parsing a Two level - array, PHP

What I have
I have two-level array as follows:
<?php $myarray = Array (
[0] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[1] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[2] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[3] => Array ( [1] => 0 [2] => 1 [3] => 0 [4] => 0 )
[4] => Array ( [1] => 0 [2] => 1 [3] => 0 [4] => 0 )
[5] => Array ( [1] => 1 [2] => 0 [3] => 0 [4] => 0 )
[6] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[7] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[8] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[9] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[10] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[11] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[12] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[13] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[14] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => red )
[15] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => yellow )
[16] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[17] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[18] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => red )
[19] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[20] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[21] => Array ( [1] => 0 [2] => 1 [3] => 0 [4] => 0 [color] => yellow )
[22] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => yellow )
[23] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => yellow )
[24] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[25] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => red )
[26] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => yellow )
) ?>
The Inner arrays (index 0 - 26 ) contain 4 or 5 entries each corresponding to positions 1,2,3,4 and entry 5 which can be red, yellow or green. A position can be 0 or 1.
What I want to do:
1) I want to add all values at position 1, position 2, position 3 and position 4 of the inner arrays (Total of a column )
2) If any inner array has more than one "position" that is 1, the whole "row" (entry) needs to be flagged and "unset from $myarray
If it is not clear, please ask some more and I will try my best to clarify.
Thanks
total of a column is pretty simple by using array_sum() combined with array_column():
$position1Sum = array_sum(array_column($myarray,'1'));
$position2Sum = array_sum(array_column($myarray,'2'));
$position3Sum = array_sum(array_column($myarray,'3'));
$position4Sum = array_sum(array_column($myarray,'4'));
to clean the array of anything with more than one position active:
/* editing the same array you are looping thru is typically bad - lets make a copy first.*/
$cleanArray = $myarray;
/* loop thru - and unset the rows where more than one position are 1
(which will make their sum more than 1) */
foreach($myarray as $key => $value){
if($value[1] + $value[2] + $value[3] +$value[4] > 1){
unset($cleanArray[$key]);
}
}
$cleanArray will now contain the same contents as $myarray, minus the rows that have more than one position set to 1. Of course there are several other ways to potentially tackle this - I try to stick to the cleanest to read later if possible... :-)

Filtering array result

My array :
<?php
$hoppa = array
(
"0" => array
("0","0","0","0","0","0","0","0","0","0"),
"1" => array
("0","0","0","0","0","0","0","0","0","0"),
"2" => array
("1","0","0","1","0","0","0","0","0","0"),
"3" => array
("1","0","0","1","0","1","1","1","1","0"),
"4" => array
("1","1","1","1","0","0","0","0","1","0"),
"5" => array
("1","0","0","1","0","1","1","1","1","0"),
"6" => array
("1","0","0","1","0","1","0","0","1","0"),
"7" => array
("1","0","0","1","0","1","1","1","1","0"),
"8" => array
("0","0","0","0","0","0","0","0","0","0"),
"9" => array
("0","0","0","0","0","0","0","0","0","0")
);
?>
My array's output:
Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[1] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[2] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[3] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 0
)
[4] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 1
[9] => 0
)
[5] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 0
)
[6] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 0
[7] => 0
[8] => 1
[9] => 0
)
[7] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 0
)
[8] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[9] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
)
it will give 0 (zero) as result
echo $hoppa[1][1];
it will give 1 (one) as result
echo $hoppa[2][0];
I dont want 0 (zero) values to show in the results. I want php script to return only 1 as results. Which functions should use? or can you give me a sample?
Use:
foreach($hoppa as $k => $v) {
$hoppa[$k] = array_filter($v);
}
This results in:
php > print_r($hoppa);
Array
(
[0] => Array
(
)
[1] => Array
(
)
[2] => Array
(
[0] => 1
[3] => 1
)
[3] => Array
(
[0] => 1
[3] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
)
[4] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[8] => 1
)
[5] => Array
(
[0] => 1
[3] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
)
[6] => Array
(
[0] => 1
[3] => 1
[5] => 1
[8] => 1
)
[7] => Array
(
[0] => 1
[3] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
)
[8] => Array
(
)
[9] => Array
(
)
)

Categories