PHP-多维数组的自定义输出


PHP - Custom output for a multi-dimensional array

在PHP中,我有一个名为$listing的二维数组,它将包含具有以下结构的数据:

(
    [1] => Array
        (
            [category] => tech
            [business_name] => Apple
        )
    [2] => Array
        (
            [category] => food
            [business_name] => McDonalds
        )
    [3] => Array
        (
            [category] => tech
            [business_name] => Dell
        )
)

我想以纯文本形式输出,按类别(按字母顺序排列)和*business_name*(按字母排序)分组。请注意,这只是它将如何显示的一个子集——可能有50个类别和1000个列表——所以代码需要考虑到这一点。

因此,使用上面列出的$listing的输出,我需要它输出如下:

category: food
business_name: McDonalds
category: tech
business_name: Apple
business_name: Dell

请帮忙。提前谢谢。

有很多方法可以做到这一点,但这应该让你开始。

$data = array();
foreach ($listing as $item) {
    $data[$item['category']][] = $item['business_name'];
}
ksort($data);
$data = array_map(function($names) {
    sort($names);
    return $names;
}, $data);

未测试。。。

了解PHP多数组排序http://www.php.net/manual/en/function.array-multisort.php要对数组进行多重排序,请查看这是否有助于

我使用了array_multsort()作用看看下面的代码。

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