简体   繁体   中英

applying array_unique

I have code to get the address which is separated by space and then fetching area details about that address so each time sql result is stored in array "resultArray" and that result is pushed to another array "returnArray" which is then displayed in the format of json.I want to remove duplicate area_id in returnArray so I used "array_unique" but it's not working .Please give some suggestion.

Sample Code:

    <?php
    include_once 'main.php';
    $dac = new Main();
    $add = $_POST['address'];
    $noLines = sizeof($add);
    $resultArray=array();
    $returnArray=array();
    $returnArrayMain=array();

    while ($noLines>0)
    {
        $resultArray=array();
        $result = $dac->area_detail($add[$noLines-1]);
        $count=mysql_num_rows($result);

     while($row = mysql_fetch_assoc($result))
     {  
            $resultArray[]=array('area_id' => $row['area_id'],'area_name' =>       $row['area_name'],'area_GISlat'=>$row['area_GISlat'],'area_GISlon'=>$row['area_GISlon']);
     }
          array_push($returnArray, $resultArray) ;  
          $noLines = $noLines-1; 

    }

      $returnArrayMain = array_unique($returnArray);
      echo json_encode($returnArrayMain);
 ?>

try this..

$returnArrayMain = uniqueAssocArray($returnArray, 'area_id');
echo json_encode($returnArrayMain);

function uniqueAssocArray($array, $uniqueKey) {
    if (!is_array($array)) {
        return array();
    }
    $uniqueKeys = array();
    foreach ($array as $key => $item) {
        $groupBy=$item[$uniqueKey];
        if (isset( $uniqueKeys[$groupBy]))
        {
            //compare $item with $uniqueKeys[$groupBy] and decide if you 
            //want to use the new item
            $replace= ... 
        }
        else
        {
            $replace=true;
        }
        if ($replace) $uniqueKeys[$groupBy] = $item;   
    }
    return $uniqueKeys;
}

尝试这个

$returnArrayMain = array_map("unserialize", array_unique(array_map("serialize", $resultArray)));

Here is solution with a testing associative array :

// this is testing array as you are using:
$resultArray = array(   
    array(
    'area_id' => 12,
    'area_name' => 'test',
    'area_GISlat'=>'test2',
    'area_GISlon'=>'test3'),
    array(
    'area_id' => 11,
    'area_name' => 'test',
    'area_GISlat'=>'test2',
    'area_GISlon'=>'test3'),
    array(
    'area_id' => 12,
    'area_name' => 'test',
    'area_GISlat'=>'test2',
    'area_GISlon'=>'test3')
    );

// take a temporary arry
$temporaryArr  = array();

// initialize key's array
$arrayKey  = array();

foreach ( $resultArray as $key => $values ) {
    if ( !in_array($values, $temporaryArr) ) {
        $temporaryArr[] = $values; // store values in temporary array
        $arrayKey[$key] = true; // store all keys in another array
    }
}

// now use array_intersect_key function for intersect both array.
$requiredArr = array_intersect_key($resultArray, $arrayKey);

echo "<pre>";
print_r($requiredArr);

Result:

Array
(
    [0] => Array
        (
            [area_id] => 12
            [area_name] => test
            [area_GISlat] => test2
            [area_GISlon] => test3
        )

    [1] => Array
        (
            [area_id] => 11
            [area_name] => test
            [area_GISlat] => test2
            [area_GISlon] => test3
        )    
)

Removed duplicate arrays .

From PHP Manual:

array_intersect_key — Computes the intersection of arrays using keys for comparison

Side note:

Also add error reporting to the top of your file(s) right after your opening <?php tag

error_reporting(E_ALL); 
ini_set('display_errors', 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM