Search code examples
phparraysmultidimensional-arrayunique

Case insensitive unique multidimensional array loop


I'm trying to loop through a multidimensional array and retreive unique values(case insensitive), can anyone help me?

$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]',
            )
        );

This what i'm trying to create:

General Enquiries 
02085237289
[email protected]

Not enquiries
[email protected]
02039303039 

What I've tried so far:

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

Solution

  • I figured things out in the end thanks to Rasclatt and Haotian Liu. I thought I should put it up just in case people were curious. Thanks guys!

    I changed the array a bit, this is how it looked:

    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>';
                            }
                        }
    
                    }