1

I have make a database query with larval.

QUERY :

if(Request::ajax()) {
        //On récupère l'ensemble des genres des séries visionnées par l'utilisateur
        $genre_user = DB::table('genres')->select('genres.name')
                                         ->join('seriesgenres', 'genres.id', '=', 'seriesgenres.genre_id')
                                         ->join('series', 'seriesgenres.series_id', '=', 'series.id')
                                         ->join('usersseries', 'series.id', '=', 'usersseries.serie_id')
                                         ->where('usersseries.user_id', '=', Auth::user()->id)->get();
        print_r($genre_user);die;
    }

After this query I have this array

Illuminate\Support\Collection Object
(
[items:protected] => Array
    (
        [0] => stdClass Object
            (
                [name] => Drama
            )

        [1] => stdClass Object
            (
                [name] => Action
            )

        [2] => stdClass Object
            (
                [name] => Crime
            )

        [3] => stdClass Object
            (
                [name] => Drama
            )

        [4] => stdClass Object
            (
                [name] => Action & Adventure
            )

        [5] => stdClass Object
            (
                [name] => Drama
            )
    )

)

I would like to count the number of each values in this array. To do that i know the array_count_values() fonction but it doesn't work on my result array. The problem come from this line :

$counts = array_count_values($genre_user);
1
  • What is the problem? Please give the error you are experiencing. Commented Jan 1, 2017 at 19:57

1 Answer 1

2

You can't do it. The function array_count_values works only for string or integer:

Throws E_WARNING for every element which is not string or integer.

For complex objects like this, you need to take a different method. Since your PHP doesn't output the warning messages, it is not getting displayed. If you want, you can do something like a transformation.

$finalItems = array();
// Loop through the items of the object and push the names into the array.
// ... Logic ...
// And finally do:
array_count_values($finalItems);
Sign up to request clarification or add additional context in comments.

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.