1

In PHP I have a two-dimensional array called $listing that will contain data with the following structure:

(
    [1] => Array
        (
            [category] => tech
            [business_name] => Apple
        )

    [2] => Array
        (
            [category] => food
            [business_name] => McDonalds
        )

    [3] => Array
        (
            [category] => tech
            [business_name] => Dell
        )

)

I want to output this in plain text grouped by category (ordered alphabetically) and then *business_name* (ordered alphabetically). Note that this is just a subset of how this will display - there could be 50 categories and 1000's of listings - so the code needs to take that into account.

So using the output for $listing as outlined above I would need it to output like the following:

category: food
business_name: McDonalds

category: tech
business_name: Apple
business_name: Dell

Please Help. Thanks in advance.

1
  • 4
    Although the solution to this problem is quite easy, I'd like to know what have you tried so far Commented Feb 24, 2013 at 5:50

3 Answers 3

2

There are a ton of ways to do this but this should get you started.

$data = array();

foreach ($listing as $item) {
    $data[$item['category']][] = $item['business_name'];
}

ksort($data);

$data = array_map(function($names) {
    sort($names);
    return $names;
}, $data);

Untested...

Sign up to request clarification or add additional context in comments.

Comments

0

Have a look at PHP multiarray sort http://www.php.net/manual/en/function.array-multisort.php to multi-sort your array, see if that helps

Comments

0

I have used array_multisort() function. Look at below code .

                         <?php

                //array to sort 
                $data[0] = array('category'=> 'tech','business_name' => 'apple')  ;
                $data[1] = array('category'=> 'food','business_name' => 'McDonalds')  ;
                $data[2] = array('category'=> 'tech','business_name' => 'dell')  ;
                $data[3] = array('category'=> 'food','business_name' => 'subway')  ;

                foreach($data as $key => $val){
                 $cat[$key] = $val['category'];
                 $bus[$key] = $val['business_name'];
                }

                // Sort the data with category and business_name with ascending order
                array_multisort($cat, SORT_ASC, $bus, SORT_ASC, $data);
       //         echo "<pre>";print_r($data);echo "</pre>";unset($val);

                //put the category as key And business name as value with comma seperated.
                $category = array();
                foreach($data as $key => $val){
                    if(array_key_exists($val['category'],$category ))
                    {
                        $category[$val['category']] = $category[$val['category']].', '.$val['business_name'];
                    }
                    else
                    {
                        $category[$val['category']] = $val['business_name']; // $category 
                    }   
                }

                //print the sorted data

                foreach($category as $key => $val){
                    //print category
                    echo "<br /> <br />Category: ".$key;
                    $b_name = explode(',', $val);
                    //to print busniess name
                    foreach($b_name as $k => $v){
                        echo "<br />Business Name: ".$v;
                    }
                 }


                ?>




        //OUTPUT


       Category: food
       Business Name: McDonalds
       Business Name: subway

       Category: tech
       Business Name: apple
       Business Name: dell 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.