2017-07-03 53 views
1

我想通過多維數組循環並檢索唯一值(不區分大小寫),任何人都可以幫助我嗎?不區分大小寫的唯一多維數組循環

$shop = array(
      array(
       'Title' => "General enquiries", 
       'Phone' => 02085237289, 
      ), 
      array(
       'Title' => "general enquiries", 
       'email' => '[email protected]', 
      ), 
      array(
       'Title' => "not enquiries", 
       'Phone' => 02039303039, 
      ), 
      array(
       'Title' => "Not enquiries", 
       'email' => '[email protected]', 
      ) 
     ); 

這是我想創建:

General Enquiries 
02085237289 
[email protected] 

Not enquiries 
[email protected] 
02039303039 

我試過到目前爲止:

$res = array(); 
foreach ($shop as $each) { 
    array_push($res,strtolower($each['Title'])); 
    array_push($res,$each['email']); 
    array_push($res,$each['Phone']); 
} 

$test = array_unique($res); 

foreach($test as $t){ 
    //echo $t; 
} 
+0

的一個問題是,你如何決定哪些選擇呢?對於'General Enquiries'你選擇了前一個,而對於'Not enquiries'你選擇了後者。 –

+0

哪種情況可供選擇?我不介意,它可能是一個,它只需要是名稱和聯繫人。 – user3053151

回答

0

感謝Rasclatt和劉浩天,我想出了最後的結果。我想我應該把它放在一起,以防萬一有人好奇。 謝謝你們!

我改變了排列了一下,這是怎麼看:

Array 
(
    [0] => Array 
     (
      [contact_description] => Employment support 
      [contact_type] => Phone 
      [contact] => 0300 456 8110 
     ) 

    [1] => Array 
     (
      [contact_description] => General enquiries 
      [contact_type] => Phone 
      [contact] => 0300 456 8052 
     ) 

    [2] => Array 
     (
      [contact_description] => employment support 
      [contact_type] => Email 
      [contact] => [email protected] 
     ) 

    [3] => Array 
     (
      [contact_description] => general enquiries 
      [contact_type] => Email 
      [contact] => [email protected] 
     ) 

) 

$res = array(); 
foreach ($shop as $each) { 
     $lcValue = strtolower($each['Title']); 
     if (isset($res[$lcValue])) 
      array_push($res[$lcValue], $each['contact']); 
     else 
      $res[$lcValue] = array($each['contact']); 
    } 




foreach ($res as $name => $contact) { 
        echo '<h5 class="mb-0">' . ucwords($name) . '</h5>'; 
        foreach ($contact as $contact) { 
         if (1 === preg_match('~[0-9]~', $contact)) { 
          // Phone Number 
          echo '<li class="work_number"><a href="tel:' . $contact . '">' . $contact . '</a></li>'; 
         } elseif (strpos($contact, '@') !== false) { 
          //Email 
          echo '<li class="email"><a href="mailto:' . $contact . '" target="_blank">' . $contact . '</a></li>'; 
         } else { 
          echo '<li><a>' . $contact . '</a></li>'; 
         } 
        } 

       } 
+0

如果這是被接受的答案,您也可以接受您的答案。那麼這個問題就不會被指定爲「開放」。如果你感到慷慨,我的回答很有幫助,那麼可以讚賞,但不是必要的......但我只是接受你自己的回答。 – Rasclatt

1

一種方法來完成,這是兩個陣列,一個存儲原始值和一個存儲小寫比較:

# Create comparison array 
$compare = array(); 
# Create a final store array 
$store  = array(); 
# Loop main rows 
foreach($shop as $row) { 
    # Loop rows (don't hardcode, it may change later) 
    foreach($row as $key => $value) { 
     # Since case is fine, you can turn all to lower for comparison 
     $lcValue = strtolower($value); 
     # Check if not in comparison array already 
     if(!in_array($lcValue,$compare)) { 
      # If not, add lowercase version to $compare and add original to $store 
      $store[] = $value; 
      $compare[] = $lcValue; 
     } 
    } 
} 

print_r($store); 

爲您提供:

Array 
(
    [0] => General enquiries 
    [1] => 02085237289 
    [2] => [email protected] 
    [3] => not enquiries 
    [4] => 02039303039 
    [5] => [email protected] 
) 

一個需要注意的是,你是怎麼知道該存儲的版本,在你的陣列的順序,不會得到Not enquiries大寫版本,因爲較低的情況下版本的運行首先在循環。你的例子有大寫,但你說它可以不區分大小寫,所以我認爲它很好...

+0

這可以工作,現在我想起來了,謝謝。 – user3053151