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条回答 默认 最新

相关推荐 更多相似问题