如何使用array_count_values函数

时间:2017-01-01 18:39:07

标签: php arrays laravel

我使用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;
    }

在此查询之后我有这个数组

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

)

我想计算这个数组中每个值的数量。 要做到这一点,我知道array_count_values()函数,但它不能在我的结果数组上工作。问题来自这条线:

$counts = array_count_values($genre_user);

1 个答案:

答案 0 :(得分:2)

你不能这样做。功能array_count_values仅适用于integerE_WARNING

  

为每个非字符串或整数的元素抛出$finalItems = array(); // Loop through the items of the object and push the names into the array. // ... Logic ... // And finally do: array_count_values($finalItems);

对于像这样的复杂对象,您需要采用不同的方法。由于您的PHP不输出警告消息,因此不会显示它。如果你愿意,你可以做一些改造。

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.swing.*;

public class Main extends JPanel {
    private static Image white;
    private static Image black;
    private static Image finish;
    private static Image player;
    private static int x = 1;
    private static int y = 1;
    private String matrix[][];

    public Main() {
        addKeyListener(new Keys());
        setFocusable(true);
    }

    public static String[][] load(String input) {
        List<String[]> rows = new ArrayList<>();
        try (Scanner scanner = new Scanner(input)) {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String[] cols = new String[line.length()];
                for (int i = 0; i < cols.length; i++) {
                    cols[i] = line.substring(i, i + 1);
                }
                rows.add(cols);
            }
        }
        return rows.toArray(new String[rows.size()][]);
    }

    public static JFrame buildFrame() {
        JFrame frame = new JFrame("Labyrinth Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(900, 950);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        return frame;
    }

    public void moveUp() {
        x += 0;
        y += -1;
    }

    public void moveLeft() {
        x += -1;
        y += 0;
    }

    public void moveDown() {
        x += 0;
        y += 1;
    }

    public void moveRight() {
        x += 1;
        y += 0;
    }

    public class Keys extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            int keycode = e.getKeyCode();
            // repaint();
            if (keycode == KeyEvent.VK_W) {
                if (!matrix[getX()][getY() - 1].equals("1")) {
                    moveUp();
                }
            }
            if (keycode == KeyEvent.VK_A) {
                if (!matrix[getX() - 1][getY()].equals("1")) {
                    moveLeft();
                }
            }
            if (keycode == KeyEvent.VK_S) {
                if (!matrix[getX()][getY() + 1].equals("1")) {
                    moveDown();
                }
            }
            if (keycode == KeyEvent.VK_D) {
                if (!matrix[getX() + 1][getY()].equals("1")) {
                    moveRight();
                }
            }
        }

        @Override
        public void keyReleased(KeyEvent event) {
        }
    }

    public static void main(String[] args) {
        String input = "1111111111111111111111111111111111111111111\n"
                + "1000000010001000001000000010000000100000001\n"
                + "1010111010101010101111101011111010111111101\n"
                + "1010001010100010100000001010000010000010001\n"
                + "1011101010111110101111111010111111111010111\n"
                + "1000101010100000101000001000100010000010001\n"
                + "1011101011101011111011101111111010111110101\n"
                + "1010001000001010000010100000001010000010101\n"
                + "1010111111111010111110111111101011111011101\n"
                + "1010100000100010100000000000101000000000101\n"
                + "1110101111101110111110111011101011111110101\n"
                + "1000100000000010000010100010001000100010001\n"
                + "1011111111111111111011101010111111101011101\n"
                + "1000000000000000100010001010000000001010001\n"
                + "1011111111111011101110111011111111111010111\n"
                + "1000100010001000001010001000100000001010101\n"
                + "1110101011101111111010101110111110111010101\n"
                + "1000101010001000100000101000100000100010001\n"
                + "1011101010111010101111101011101110101111111\n"
                + "1000001010000010000000101000001000100010001\n"
                + "1111111011111110111111101111111011111010101\n"
                + "1000001010000010100010001000000010000010101\n"
                + "1011111010111011101010111011111110101110101\n"
                + "1010000010001010001010001000100000101010101\n"
                + "1010111111101010111011101111101111101011101\n"
                + "1000100000001010101010001000100010101000101\n"
                + "1011111011111010101010111010111010101011101\n"
                + "1010000010001000101010000010001010001000001\n"
                + "1010101110101111101011101111101011111010101\n"
                + "1010101000101000001000101000001000000010101\n"
                + "1011101011111010111110111011101111111110111\n"
                + "1000001000000010000000000010000000000010021\n"
                + "1111111111111111111111111111111111111111111\n";

        String[][] matrix = load(input);

        JFrame frame = buildFrame();
        ImageIcon img = new ImageIcon("C:/Users/Desktop/black20.png");
        black = img.getImage();
        img = new ImageIcon("C:/Users/Desktop/gri20.png");
        white = img.getImage();
        img = new ImageIcon("C:/Users/Desktop/finish20.png");
        finish = img.getImage();
        img = new ImageIcon("C:/Users/Desktop/smiley20.png");
        player = img.getImage();
        // frame.add(new Player());
        JPanel pane = new JPanel() {
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                for (int i = 0; i < matrix.length; i++) {
                    for (int j = 0; j < matrix[0].length; j++) {
                        if (matrix[i][j].equals("1")) {
                            g.drawImage(black, i * 20, j * 20, null);
                        }
                        if (matrix[i][j].equals("0")) {
                            g.drawImage(white, i * 20, j * 20, null);
                        }
                        if (matrix[i][j].equals("2")) {
                            g.drawImage(finish, i * 20, j * 20, null);
                        }
                    }
                }
                g.drawImage(player, x * 20, y * 20, null);
            }
        };
        frame.add(pane);
        frame.add(new Main());
    }
}