duanfei1930 2018-11-18 16:13
浏览 55
已采纳

从多维数组中的数组中删除重复值

At the moment I have this array:

    array(4) {
    [0]=>
    array(2) {
       ["cluster"]=>
       string(3) "ICT"
       ["opleiding"]=>
       string(32) "Applicatie- en mediaontwikkeling"
    }
    [1]=>
    array(2) {
       ["cluster"]=>
       string(3) "ICT"
       ["opleiding"]=>
       string(21) "ICT cluster opleiding"
    }
    [2]=>
    array(2) {
       ["cluster"]=>
       string(15) "nog een cluster"
       ["opleiding"]=>
       string(25) "nog een cluster opleiding"
    }
    [3]=>
    array(2) {
       ["cluster"]=>
       string(8) "Techniek"
       ["opleiding"]=>
       string(26) "Techniek cluster opleiding"
   }
}      

From this array I would like to be able to remove duplicate clusters, in this case "ICT"(Could become more eventually).

This array comes from the following query:

        $result_array = $wpdb->get_results("SELECT cluster.cluster, 
        opleiding.opleiding 
        FROM opleiding 
        INNER JOIN cluster 
        ON opleiding.cluster_id = cluster.cluster_id 
        GROUP BY opleiding.opleiding", ARRAY_A);

So, what my desired array would look like:

    array(4) {
    [0]=>
    array(2) {
       ["cluster"]=>
       string(3) "ICT"
       ["opleiding"]=>
       string(32) "Applicatie- en mediaontwikkeling"
    }
    [1]=>
    array(1) {
       ["opleiding"]=>
       string(21) "ICT cluster opleiding"
    }
    [2]=>
    array(2) {
       ["cluster"]=>
       string(15) "nog een cluster"
       ["opleiding"]=>
       string(25) "nog een cluster opleiding"
    }
    [3]=>
    array(2) {
       ["cluster"]=>
       string(8) "Techniek"
       ["opleiding"]=>
       string(26) "Techniek cluster opleiding"
   }
}      

How would I approach this issue? Should I change something within my query? Or is my initial thought to remove the value from the array correct, and then how would I do this?

My output function:

public function getOpleidingCluster()
{
    global $wpdb;
    $return_array = array();
    $result_array = $wpdb->get_results("SELECT cluster.cluster, opleiding.opleiding 
    FROM opleiding 
    INNER JOIN cluster 
    ON opleiding.cluster_id = cluster.cluster_id 
    GROUP BY opleiding.opleiding", ARRAY_A);



    echo "<pre>";
    var_dump($result_array);
    echo "   </pre>";
    // For all database results:
    foreach ($result_array as $idx => $array) {
        // New opleiding object
        $koppelOpleidingCluster = new ClusterOpleiding();
        // Set all info
        $koppelOpleidingCluster->setCluster($array['cluster']);
        $koppelOpleidingCluster->setClusterId($array['cluster_id']);
        $koppelOpleidingCluster->setOpleidingId($array['opleiding_id']);
        $koppelOpleidingCluster->setOpleiding($array['opleiding']);
        // Add new object to return array.
        $return_array[] = $koppelOpleidingCluster;
    }
    return $return_array;
}

Getting the data on the page:

        <table border="1">
        <?php
        $koppelOpleidingCluster = $clusterOpleiding->getOpleidingCluster();
        foreach ($koppelOpleidingCluster as $koppelOpleidingCluster2){
            ?>
            <tr>
                <td style="display:none;"> <input type="hidden" name="clusterId" value=" <?php echo $koppelOpleidingCluster2->getClusterId(); ?>"></td>
                <td name="cluster"> <?php echo $koppelOpleidingCluster2->getCluster(); ?></td>
            </tr>
            <tr>
                <td style="display:none;"> <input type="hidden" name="opleidingId" value=" <?php echo $koppelOpleidingCluster2->getOpleidingId(); ?>"></td>
                <td name="opleiding"> <?php echo $koppelOpleidingCluster2->getOpleiding(); ?></td>
            </tr>
            <?php

        }
        ?>
        </table>

opleiding means education

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • doujiene2845 2018-11-18 16:42
    关注

    The desired array structure you want to get to is not really something I would advise to go for. It is best practice that all elements in the array have the same keys.

    You seem to look for a way to suppress repeated values in your output. It is a mistake to try to make your array match your desired output. Instead leave the array as it is, and only make your code that iterates it aware of repeating values, so that it will not output them.

    Before getting to that, there is another point to make about your SQL query. It violates a rule that if you have a group by clause, the select clause should only have either aggregated expressions or expressions that occur in the group by clause. In this particular case it seems to me that you can skip the group by clause. Instead add an order by cluster.cluster, opleiding.opleiding clause so that it is guaranteed that duplicate clusters will appear without interruption. Or to say it differently: it guarantees that all educations belonging to the same cluster are listed together.

    Now to the altered output code. Use an extra variable that will be used to detect repeating values, and add an if block that will only display the cluster row when it is not a repetition:

    <?php
        $prevCluster = ""; // <----- add this
        $koppelOpleidingCluster = $clusterOpleiding->getOpleidingCluster();
        foreach ($koppelOpleidingCluster as $koppelOpleidingCluster2){
            if ($koppelOpleidingCluster2->getClusterId() != $prevCluster) { // <----
    ?>
            <tr>
                <td style="display:none;"> <input type="hidden" name="clusterId" value=" <?php echo $koppelOpleidingCluster2->getClusterId(); ?>"></td>
                <td name="cluster"> <?php echo $koppelOpleidingCluster2->getCluster(); ?></td>
            </tr>
    <?php
            } // <-----
            $prevCluster = $koppelOpleidingCluster2->getClusterId(); // <-----
    ?>
            <tr>
                <td style="display:none;"> <input type="hidden" name="opleidingId" value=" <?php echo $koppelOpleidingCluster2->getOpleidingId(); ?>"></td>
                <td name="opleiding"> <?php echo $koppelOpleidingCluster2->getOpleiding(); ?></td>
            </tr>
    <?php
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?