Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Sorting a multi-dimensional array(Case Insensitive)

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

$shop = array(
            array(
                'Title' => "General enquiries", 
                'Phone' => 02085237289,
            ),
            array(
                'Title' => "general enquiries", 
                'email' => 'something@gmail.com',
            ),
            array(
                'Title' => "not enquiries", 
                'Phone' => 02039303039,
            ),
            array(
                'Title' => "Not enquiries", 
                'email' => 'blah@gmail.com',
            )
        );

This what i'm trying to create:

General Enquiries 02085237289 something@gmail.com

Not enquiries blah@gmail.com 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;
}

1 Answer

Antonio De Rose
Antonio De Rose
20,885 Points

this is a very good question, wish am able to upvote you for the question, you have raised.

<?php

$join = array();
for ($i = 0; $i < sizeof($shop)-1; $i++){
    if ((strtoupper($shop[$i]['Title'])) == (strtoupper($shop[$i+1]['Title']))){
                $join[] = array(
                    $shop[$i]['Title'],
                    $shop[$i]['Phone'],
                    $shop[$i+1]['email']
                );
    }
}
print_r($join);

?>