使用PHP数组分组时遇到问题[关闭]

j5fpnvbx  于 2023-05-16  发布在  PHP
关注(0)|答案(1)|浏览(122)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
5天前关闭。
Improve this question
我的PHP代码有问题,但我似乎找不到正确的解决方案。基本上,我有一个数据库,我试图使它,以便我组的点值分配给ID。为了理解我的意思,下面是我运行程序时遇到的问题:

在左边,你有我做的ID。右侧是分配给ID的点值。请注意,所有的点都是分组的,但并不完全是我想要的,例如ID-10935的点总数是7136个点,但输出结果却像那样混乱。下面是我目前的代码:

public function index()
    {
        $this->checkCookie();
        
        $payload = ( !empty($this->request->getVar('dataKlinik')) ? $this->request->getVar('dataKlinik') : "" );

        $filter = [];
        $dataPointUser = $this->GeneralNoSQL->get_data_all_2('db_rewards','point_reward_logs', $filter);
        $dataKaryawan = $this->poinUserModel->getKaryawan($payload);
        
        $arrayData = [];
        $poinGrouping = [];

        foreach($dataPointUser as $d){
            $namaKaryawan = "";
            foreach($dataKaryawan as $k){
                if($k->ID_KARYAWAN == $d->id_karyawan){
                    $namaKaryawan = $k->NAMA;
                    break;
                }
            }

            if(isset($poinGrouping[$d->id_karyawan])){
                $poinGrouping[$d->id_karyawan] += $d->jumlah_poin;
            } else {
                $poinGrouping[$d->id_karyawan] = $d->jumlah_poin;
            }
        
            $listData = new \stdClass();
            $listData->id_karyawan = $d->id_karyawan;
            $listData->karyawan = $namaKaryawan;
            $listData->jumlah_poin = $poinGrouping[$d->id_karyawan];

            $arrayData[] = $listData;
        }

        $data['payload'] = $payload;
        $data['dataPoint'] = $arrayData;
        $data['page'] = 'Pegawai/managePointUser';

        return view('MainDashboard/index', $data);
    }

如果你觉得我有什么需要写的话,我的观点是:

<div class="app-main flex-column flex-row-fluid" id="kt_app_main">
    <div class="d-flex flex-column flex-column-fluid">
        <div id="kt_app_toolbar" class="app-toolbar py-3 py-lg-6">
            <div id="kt_app_toolbar_container" class="app-container container-xxl d-flex flex-stack">
                <div class="page-title d-flex flex-column justify-content-center flex-wrap me-3">
                    <h1 class="page-heading d-flex text-dark fw-bold fs-3 flex-column justify-content-center my-0">
                        Poin User
                    </h1>
                </div>
            </div>
        </div>
        <div id="kt_app_content" class="app-content flex-column-fluid">
            <div id="kt_app_content_container" class="app-container container-xxl">
                <div class="card card-flush">
                    <div class="card-header align-items-center py-5 gap-2 gap-md-5">
                        <div class="card-toolbar">
                            <input type="text" class="form-control form-control-solid w-250px ps-14" placeholder="Search User" id="searchUser" />
                        </div>
                        <div class="card-toolbar flex-row-fluid justify-content-end gap-5">
                            <button 
                                type="button" 
                                class="btn btn-primary mr-3" 
                                data-bs-toggle="modal" 
                                data-bs-target="#poinFilter">
                                <span class="svg-icon svg-icon-6 svg-icon-muted me-1">
                                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                        <path d="M19.0759 3H4.72777C3.95892 3 3.47768 3.83148 3.86067 4.49814L8.56967 12.6949C9.17923 13.7559 9.5 14.9582 9.5 16.1819V19.5072C9.5 20.2189 10.2223 20.7028 10.8805 20.432L13.8805 19.1977C14.2553 19.0435 14.5 18.6783 14.5 18.273V13.8372C14.5 12.8089 14.8171 11.8056 15.408 10.964L19.8943 4.57465C20.3596 3.912 19.8856 3 19.0759 3Z" fill="currentColor" />
                                    </svg>
                                </span>
                                Filter
                            </button>
                            <div id="customButtonTemplate"></div>
                        </div>
                    </div>
                    <div class="card-body pt-0">
                        <div class="table-both-scroll">
                            <table class="table table-striped table-hover order-column table-row-dashed fs-6 gy-5 nowrap" id="poinTable">
                                <thead>
                                    <tr>
                                        <td class="text-center align-middle">No</td>
                                        <td class="text-center align-middle">ID Karyawan</td>
                                        <td class="text-center align-middle">Nama Karyawan</td>
                                        <td class="text-center align-middle">Point</td>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                        $no = 1;
                                        foreach($dataPoint as $d){
                                            echo "
                                                <tr>
                                                    <td class='text-center'>$no</td>
                                                    <td class='text-center'>$d->id_karyawan</td>
                                                    <td class='text-center'>$d->karyawan</td>
                                                    <td class='text-center'>$d->jumlah_poin</td>
                                                </tr>
                                            ";
                                            $no++;
                                        }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

我现在完全被卡住了,因为我似乎找不到解决这个问题的办法。我对PHP相当陌生,因为我以前从未学习过如何涉足数组,我发现其他问题并没有真正回答我的问题,希望有一些帮助,也许是我当前代码的修改版本,如果可能的话,以供将来参考!
下面是我期望的输出:

2ul0zpep

2ul0zpep1#

没关系,我只需要像这样添加另一个foreach:

public function index()
    {
        $this->checkCookie();
        
        $payload = ( !empty($this->request->getVar('dataKlinik')) ? $this->request->getVar('dataKlinik') : "" );

        $filter = [];
        $dataPointUser = $this->GeneralNoSQL->get_data_all_2('db_rewards','point_reward_logs', $filter);
        $dataKaryawan = $this->poinUserModel->getKaryawan($payload);
        
        $arrayData = [];
        $poinGrouping = [];

        foreach($dataPointUser as $d){
            $namaKaryawan = "";
            foreach($dataKaryawan as $k){
                if($k->ID_KARYAWAN == $d->id_karyawan){
                    $namaKaryawan = $k->NAMA;
                    break;
                }
            }

            if(isset($poinGrouping[$d->id_karyawan])){
                $poinGrouping[$d->id_karyawan] += $d->jumlah_poin;
            } else {
                $poinGrouping[$d->id_karyawan] = $d->jumlah_poin;
            }

        foreach($poinGrouping as $karyawanId => $jumlahPoin){
            $listData = new \stdClass();
            $listData->id_karyawan = $karyawanId;
            $listData->karyawan = $namaKaryawan;
            $listData->jumlah_poin = $jumlahPoin;
        
            $arrayData[] = $listData;
        }

        $data['payload'] = $payload;
        $data['dataPoint'] = $arrayData;
        $data['page'] = 'Pegawai/managePointUser';

        return view('Dashboard/index', $data);
    }

返回id_karyawan的单个值和分配给它的jumlah_poin值的总和。感谢所有的建议!

相关问题