0

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 !

1
  • 1
    This is javascript switch ($value.niveau) what you want is for php switch ($value['niveau']) Commented Jul 3, 2014 at 8:30

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

9 Comments

Hello ! It doesn't work but it's nearly the good result I think :) . I edit my post regarding the Object array... Is it change something ?
@Zagloo Before update the answer, what do you want to push index, video level? video id? So what contains $NIV array?
The finality of this code is to do a weighted average of all Video Levels. I put the rest of my code now. So I just need to push level of each video, count how many of each different level there are and do a weighted average
@Zagloo Updated answer with new named variables like $Chapter->snapshot. Sure this variables
I cant help you in this way. always not working Not working what? Do you get any errors? Did you dump it var_dump($NIV);
|
0

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 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.