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
        }
    ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doute7910 2018-11-18 16:23

    array_unique($array, SORT_REGULAR) use this

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题