Remove duplicates from PHP array

后端 未结 2 866
离开以前
离开以前 2021-01-29 15:46

My PHP script is returning some array and I would like to remove duplicates in that array. Can you please tell me how? I dont want to remove duplicates from MySQL, just from PHP

相关标签:
2条回答
  • 2021-01-29 16:21

    Replace this:

    $rows = (object) array('lista' => $rows);
    echo json_encode($rows);
    

    With this:

    $lista = array_map('unserialize', array_unique(array_map('serialize', $rows)));
    echo json_encode((object) array('lista' => $lista));
    
    0 讨论(0)
  • 2021-01-29 16:40

    try this function http://php.net/manual/en/function.array-unique.php

    array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
    

    Exemple from the php.net documentation :

    <?php
        $input = array("a" => "green", "red", "b" => "green", "blue", "red");
        $result = array_unique($input);
        print_r($result);
    ?>
    
    0 讨论(0)
提交回复
热议问题