2016-01-13 69 views
1

我有代碼來獲取由空格分隔的地址,然後獲取有關該地址的區域詳細信息,以便每次將sql結果存儲在數組「resultArray」中並將結果推送到另一個數組「returnArray」,即然後以json格式顯示。我想在returnArray中刪除重複的area_id,所以我使用了「array_unique」,但它不起作用。請給出一些建議。應用array_unique

示例代碼:

<?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); 
?> 
+0

請看看:http://stackoverflow.com/a/2442315/3647441 –

+0

我已經嘗試過他們提到的方法,但它不工作,因爲它在兩個數組內。 – user092

回答

0

下面是測試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); 

結果:

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 
     )  
) 

重複arrays刪除。

從PHP手冊:

array_intersect_key - 使用鍵數組的交集比較

旁註:

同時添加錯誤的文件的頂部報告(S )立即開始<?php標記

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

試試這個..

$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; 
} 
+1

什麼是'uniqueAssocArray()'? – Darren

+0

sry忘了給功能..我在我的答案中添加了功能 –

0

試試這個

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