Explode array to multi array - php

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

Related

How do I combine two arrays, one as a key and the other as an array?

The output of the first array as the value for the update in the third array
Array
(
[0] => Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 1
[3] => 1
[4] => 1
[5] => 1
)
[1] => Array
(
[0] => 1
[1] => 1
[2] => 0
[3] => 0
[4] => 0
[5] => 1
)
)
The output of the second array as a key for updating the third array
Array
(
[0] => Array
(
[0] => 4
)
[1] => Array
(
[0] => 5
)
[2] => Array
(
[0] => 6
)
[3] => Array
(
[0] => 7
)
[4] => Array
(
[0] => 8
)
[5] => Array
(
[0] => 9
)
This is the third array with 48 keys
Array
(
[1] => Array
(
[1] => 1
)
[2] => Array
(
[1] => 1
)
[3] => Array
(
[1] => 1
)
[4] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 1
[10] => Array
(
[4] => 1
)
[11] => Array
(
[4] => 1
)
[12] => Array
(
[4] => 1
)
[14] => Array
(
[5] => 1
)
[15] => Array
(
[5] => 1
)
[17] => Array
(
[6] => 1
)
[18] => Array
(
[6] => 1
)
[20] => Array
(
[7] => 1
)
[21] => Array
(
[7] => 1
)
[23] => Array
(
[8] => 1
)
[24] => Array
(
[8] => 1
)
[27] => Array
(
[9] => 1
)
[29] => Array
(
[10] => 1
)
[30] => Array
(
[10] => 1
)
[32] => Array
(
[11] => 1
)
[33] => Array
(
[11] => 1
)
[34] => Array
(
[12] => 1
)
[36] => Array
(
[12] => 1
)
[38] => Array
(
[13] => 1
)
[39] => Array
(
[13] => 1
)
[42] => Array
(
[14] => 1
)
[44] => Array
(
[15] => 1
)
[45] => Array
(
[15] => 1
)
[46] => Array
(
[16] => 1
)
[47] => Array
(
[16] => 1
)
[48] => Array
(
[16] => 1
)
The problem is that I can not use one as a key and the other as a value to update the third array
Code I have tried :
foreach($arraykey as $key => $value){
for ($i=0;$i < count($array3) ; $i++){
if ($value2==$i){
$array3[$value2]=$arrayval[0][0][$i];
}
}
}
You can use array_combine(), for example like this:
<?php
$a = [1,2,3,4];
$b = [4,5,6,7];
$c = array_combine($b,$a);
print_r($c);
?>
result:
Array (
[4] => 1
[5] => 2
[6] => 3
[7] => 4 )

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... :-)

Matrix of Controller to View - ZF

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.

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