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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题