Reputation: 1387
I tried to push in array different values from multidimentionnal object array.
My object seems to be like that in 'Chapter'
:
object(Doctrine\ORM\PersistentCollection)[5301]
private 'snapshot' =>
array (size=3)
0 =>
object(Video2Learn\BddBundle\Entity\Chapitre)[5335]
private 'id' => int 1
private 'titre' => string 'C01' (length=3)
private 'Videos' =>
object(Doctrine\ORM\PersistentCollection)[5371]
...
1 =>
object(Video2Learn\BddBundle\Entity\Chapitre)[5373]
private 'id' => int 2
private 'titre' => string 'C02' (length=3)
private 'Videos' =>
object(Doctrine\ORM\PersistentCollection)[5374]
...
2 =>
object(Video2Learn\BddBundle\Entity\Chapitre)[5376]
private 'id' => int 3
private 'titre' => string 'C03' (length=3)
private 'Tutoriel' =>
private 'Videos' =>
object(Doctrine\ORM\PersistentCollection)[5377]
...
and in 'Videos'
0 =>
object(Video2Learn\BddBundle\Entity\Video)[5831]
private 'id' => int 4
private 'titre' => string 'qzesthbvs s hgqsd' (length=17)
private 'prix' => int 0
private 'level' => string 'hard' (length=8)
1 =>
object(Video2Learn\BddBundle\Entity\Video)[5831]
private 'id' => int 4
private 'titre' => string 'qzesthbvs s hgqsd' (length=17)
private 'prix' => int 0
private 'level' => string 'noob' (length=8)
Before to try in PHP I did this in Javascript :
var NIV = [0, 1, 2];
NIV[0] = [];
NIV[1] = [];
NIV[2] = [];
$.each(e.tuto.Chapiter, function(idx, chapiter) {
$.each(chapiter.Videos, function(idx, video) {
switch (video.level) {
case "noob":
NIV[0].push(video.level);
break;
case "medium":
NIV[1].push(video.level);
break;
case "hard":
NIV[2].push(video.level);
break;
}
});
});
But now I need to do the same thing in PHP. I tried to do that but I'm not expert in PHP and I think I have a problem in my loops... How to select 'Level'
?
I did that :
$NIV = [0, 1, 2];
$NIV[0] = [];
$NIV[1] = [];
$NIV[2] = [];
foreach ($chap as $section => $video) {
foreach ($video as $niveau => $value) {
switch ($value.niveau) {
case "noob":
array_push($NIV[0], ($value.level));
break;
case "medium":
array_push($NIV[1], ($value.level));
break;
case "hard":
array_push($NIV[2], ($value.level));
break;
}
}
}
For information, here is the rest of my code to do a weighted average of all levels from all Videos:
$nb_noob = sizeof($NIV[0]);
$nb_medium = sizeof($NIV[1]);
$nb_hard = sizeof($NIV[2]);
$total_niv = $nb_noob + $nb_medium + $nb_hard ;
$ratio_nb_noob = $nb_noob / $total_niv;
$ratio_nb_medium = $nb_medium / $total_niv;
$ratio_nb_hard = $nb_hard / $total_niv;
$weighted_average= $ratio_nb_noob * 1 + $ratio_nb_medium * 2 + $ratio_nb_hard * 3;
$weighted_average= round($weighted_average);
thanks !
Upvotes: 0
Views: 70
Reputation: 1387
This is the solution :)
$NIV = [0, 1, 2];
$NIV[0] = 0;
$NIV[1] = 0;
$NIV[2] = 0;
foreach ($chapter as $key => $value) {
foreach ($value->getVideos() as $index => $val) {
switch ($val->getLevel()) {
case "noob":
$NIV[0] ++;
break;
case "medium":
$NIV[1] ++;
break;
case "hard":
$NIV[2] ++;
break;
}
}
}
$total_niv = $NIV[0] + $NIV[1] + $NIV[2];
$ratio_nb_noob = $NIV[0] / $total_niv;
$ratio_nb_medium = $NIV[1] / $total_niv;
$ratio_nb_hard = $NIV[2] / $total_niv;
$weighted_average = $ratio_nb_noob * 1 + $ratio_nb_medium * 2 + $ratio_nb_hard * 3;
$weighted_average = round($weighted_average );
Upvotes: 0
Reputation: 10717
I guess you want something like this:
$NIV = [0, 1, 2];
$NIV[0] = 0;
$NIV[1] = 0;
$NIV[2] = 0;
foreach ($Chapter->snapshot as $section => $row)
{
foreach ($row->Videos as $niveau => $video)
{
switch ($video->level)
{
case "noob":
$NIV[0]++;
break;
case "medium":
$NIV[1]++;
break;
case "hard":
$NIV[2]++;
break;
}
}
}
You can access object index with $video->level
And now you doesnt need size_of
function. Just get it with $NIV[0]
that returns count
Upvotes: 1