1
$array = (
'item'=>'1',
'category'=>'2',
'item'=>'2',
'category'=>'2',
'item'=>'3',
'category'=>'1',
'item'=>'4',
'category'=>'3'
); 

如果可能的话,我想以 html 格式返回:

Category 1
- item3
Category 2
- item1
- item2
Category 3
- item4

有什么建议吗?真的不知道从哪里开始做这个脚本:/

编辑真实数组是:

    array(39) {
  [0]=>
  object(stdClass)#50 (6) {
    ["id"]=>
    string(2) "20"
    ["name"]=>
    string(7) "youtube"
    ["url"]=>
    string(11) "youtube.com"
    ["shortname"]=>
    string(2) "yo"
    ["category"]=>
    string(5) "video"
    ["img"]=>
    string(1) "y"
  }
  [1]=>
  object(stdClass)#51 (6) {
    ["id"]=>
    string(2) "23"
    ["name"]=>
    string(5) "vimeo"
    ["url"]=>
    string(9) "vimeo.com"
    ["shortname"]=>
    string(2) "vm"
    ["category"]=>
    string(5) "video"
    ["img"]=>
    string(1) "y"
  }
  [2]=>
  object(stdClass)#52 (6) {
    ["id"]=>
    string(1) "1"
    ["name"]=>
    string(10) "googleplus"
    ["url"]=>
    string(15) "plus.google.com"
    ["shortname"]=>
    string(2) "g+"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [3]=>
  object(stdClass)#53 (6) {
    ["id"]=>
    string(1) "2"
    ["name"]=>
    string(8) "facebook"
    ["url"]=>
    string(12) "facebook.com"
    ["shortname"]=>
    string(2) "fb"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [4]=>
  object(stdClass)#54 (6) {
    ["id"]=>
    string(1) "4"
    ["name"]=>
    string(7) "twitter"
    ["url"]=>
    string(11) "twitter.com"
    ["shortname"]=>
    string(2) "tw"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [5]=>
  object(stdClass)#55 (6) {
    ["id"]=>
    string(1) "9"
    ["name"]=>
    string(9) "instagram"
    ["url"]=>
    string(13) "instagram.com"
    ["shortname"]=>
    string(2) "is"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [6]=>
  object(stdClass)#56 (6) {
    ["id"]=>
    string(2) "17"
    ["name"]=>
    string(8) "diaspora"
    ["url"]=>
    string(16) "joindiaspora.com"
    ["shortname"]=>
    string(2) "dp"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [7]=>
  object(stdClass)#57 (6) {
    ["id"]=>
    string(2) "22"
    ["name"]=>
    string(7) "myspace"
    ["url"]=>
    string(11) "myspace.com"
    ["shortname"]=>
    string(2) "my"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [8]=>
  object(stdClass)#58 (6) {
    ["id"]=>
    string(2) "32"
    ["name"]=>
    string(5) "orkut"
    ["url"]=>
    string(9) "orkut.com"
    ["shortname"]=>
    string(2) "ot"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [9]=>
  object(stdClass)#59 (6) {
    ["id"]=>
    string(2) "33"
    ["name"]=>
    string(8) "linkedin"
    ["url"]=>
    string(12) "linkedin.com"
    ["shortname"]=>
    string(2) "ln"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [10]=>
  object(stdClass)#60 (6) {
    ["id"]=>
    string(2) "34"
    ["name"]=>
    string(9) "delicious"
    ["url"]=>
    string(13) "delicious.com"
    ["shortname"]=>
    string(2) "dl"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [11]=>
  object(stdClass)#61 (6) {
    ["id"]=>
    string(2) "35"
    ["name"]=>
    string(4) "digg"
    ["url"]=>
    string(8) "digg.com"
    ["shortname"]=>
    string(2) "dg"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [12]=>
  object(stdClass)#62 (6) {
    ["id"]=>
    string(2) "40"
    ["name"]=>
    string(11) "stumbleupon"
    ["url"]=>
    string(15) "stumbleupon.com"
    ["shortname"]=>
    string(2) "sb"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [13]=>
  object(stdClass)#63 (6) {
    ["id"]=>
    string(2) "41"
    ["name"]=>
    string(10) "friendfeed"
    ["url"]=>
    string(14) "friendfeed.com"
    ["shortname"]=>
    string(2) "ff"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [14]=>
  object(stdClass)#64 (6) {
    ["id"]=>
    string(2) "47"
    ["name"]=>
    string(6) "tumblr"
    ["url"]=>
    string(10) "tubmlr.com"
    ["shortname"]=>
    string(2) "tu"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [15]=>
  object(stdClass)#65 (6) {
    ["id"]=>
    string(2) "48"
    ["name"]=>
    string(9) "pinterest"
    ["url"]=>
    string(13) "pinterest.com"
    ["shortname"]=>
    string(2) "pi"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [16]=>
  object(stdClass)#66 (6) {
    ["id"]=>
    string(2) "67"
    ["name"]=>
    string(11) "tripadvisor"
    ["url"]=>
    string(15) "tripadvisor.com"
    ["shortname"]=>
    string(2) "tp"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [17]=>
  object(stdClass)#67 (6) {
    ["id"]=>
    string(2) "76"
    ["name"]=>
    string(8) "dribbble"
    ["url"]=>
    string(12) "dribbble.com"
    ["shortname"]=>
    string(2) "db"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [18]=>
  object(stdClass)#68 (6) {
    ["id"]=>
    string(2) "88"
    ["name"]=>
    string(10) "foursquare"
    ["url"]=>
    string(14) "foursquare.com"
    ["shortname"]=>
    string(2) "fs"
    ["category"]=>
    string(6) "social"
    ["img"]=>
    string(1) "y"
  }
  [19]=>
  object(stdClass)#69 (6) {
    ["id"]=>
    string(1) "3"
    ["name"]=>
    string(5) "yahoo"
    ["url"]=>
    string(9) "yahoo.com"
    ["shortname"]=>
    string(2) "yh"
    ["category"]=>
    string(6) "search"
    ["img"]=>
    string(1) "y"
  }
  [20]=>
  object(stdClass)#70 (6) {
    ["id"]=>
    string(2) "36"
    ["name"]=>
    string(6) "reddit"
    ["url"]=>
    string(10) "reddit.com"
    ["shortname"]=>
    string(2) "re"
    ["category"]=>
    string(6) "search"
    ["img"]=>
    string(1) "y"
  }
  [21]=>
  object(stdClass)#71 (6) {
    ["id"]=>
    string(2) "42"
    ["name"]=>
    string(10) "craigslist"
    ["url"]=>
    string(14) "craigslist.com"
    ["shortname"]=>
    string(2) "cg"
    ["category"]=>
    string(6) "search"
    ["img"]=>
    string(1) "y"
  }
  [22]=>
  object(stdClass)#72 (6) {
    ["id"]=>
    string(2) "51"
    ["name"]=>
    string(6) "flickr"
    ["url"]=>
    string(10) "flickr.com"
    ["shortname"]=>
    string(2) "fk"
    ["category"]=>
    string(5) "photo"
    ["img"]=>
    string(1) "y"
  }
  [23]=>
  object(stdClass)#73 (6) {
    ["id"]=>
    string(2) "85"
    ["name"]=>
    string(6) "picasa"
    ["url"]=>
    string(10) "picasa.com"
    ["shortname"]=>
    string(2) "ps"
    ["category"]=>
    string(5) "photo"
    ["img"]=>
    string(1) "y"
  }
  [24]=>
  object(stdClass)#74 (6) {
    ["id"]=>
    string(2) "38"
    ["name"]=>
    string(9) "wordpress"
    ["url"]=>
    string(13) "wordpress.com"
    ["shortname"]=>
    string(2) "wp"
    ["category"]=>
    string(6) "others"
    ["img"]=>
    string(1) "y"
  }
  [25]=>
  object(stdClass)#75 (6) {
    ["id"]=>
    string(1) "5"
    ["name"]=>
    string(10) "soundcloud"
    ["url"]=>
    string(13) "souncloud.com"
    ["shortname"]=>
    string(2) "sc"
    ["category"]=>
    string(5) "music"
    ["img"]=>
    string(1) "y"
  }
  [26]=>
  object(stdClass)#76 (6) {
    ["id"]=>
    string(1) "6"
    ["name"]=>
    string(7) "spotify"
    ["url"]=>
    string(11) "spotify.com"
    ["shortname"]=>
    string(2) "sp"
    ["category"]=>
    string(5) "music"
    ["img"]=>
    string(1) "y"
  }
  [27]=>
  object(stdClass)#77 (6) {
    ["id"]=>
    string(2) "10"
    ["name"]=>
    string(6) "lastfm"
    ["url"]=>
    string(10) "lastfm.com"
    ["shortname"]=>
    string(2) "lf"
    ["category"]=>
    string(5) "music"
    ["img"]=>
    string(1) "y"
  }
  [28]=>
  object(stdClass)#78 (6) {
    ["id"]=>
    string(1) "8"
    ["name"]=>
    string(5) "gmail"
    ["url"]=>
    string(9) "gmail.com"
    ["shortname"]=>
    string(2) "gm"
    ["category"]=>
    string(5) "email"
    ["img"]=>
    string(1) "y"
  }
  [29]=>
  object(stdClass)#79 (6) {
    ["id"]=>
    string(1) "7"
    ["name"]=>
    string(13) "stackoverflow"
    ["url"]=>
    string(17) "stackoverflow.com"
    ["shortname"]=>
    string(2) "st"
    ["category"]=>
    string(10) "developers"
    ["img"]=>
    string(1) "y"
  }
  [30]=>
  object(stdClass)#80 (6) {
    ["id"]=>
    string(2) "55"
    ["name"]=>
    string(6) "github"
    ["url"]=>
    string(10) "github.com"
    ["shortname"]=>
    string(2) "gt"
    ["category"]=>
    string(10) "developers"
    ["img"]=>
    string(1) "y"
  }
  [31]=>
  object(stdClass)#81 (6) {
    ["id"]=>
    string(2) "54"
    ["name"]=>
    string(7) "dropbox"
    ["url"]=>
    string(11) "dropbox.com"
    ["shortname"]=>
    string(2) "do"
    ["category"]=>
    string(4) "apps"
    ["img"]=>
    string(1) "y"
  }
  [32]=>
  object(stdClass)#82 (6) {
    ["id"]=>
    string(2) "59"
    ["name"]=>
    string(5) "skype"
    ["url"]=>
    string(9) "skype.com"
    ["shortname"]=>
    string(2) "sk"
    ["category"]=>
    string(4) "apps"
    ["img"]=>
    string(1) "y"
  }
  [33]=>
  object(stdClass)#83 (6) {
    ["id"]=>
    string(2) "72"
    ["name"]=>
    string(10) "googletalk"
    ["url"]=>
    string(10) "google.com"
    ["shortname"]=>
    string(2) "gk"
    ["category"]=>
    string(4) "apps"
    ["img"]=>
    string(1) "y"
  }
  [34]=>
  object(stdClass)#84 (6) {
    ["id"]=>
    string(2) "81"
    ["name"]=>
    string(10) "googleplay"
    ["url"]=>
    string(15) "play.google.com"
    ["shortname"]=>
    string(2) "gp"
    ["category"]=>
    string(4) "apps"
    ["img"]=>
    string(1) "y"
  }
  [35]=>
  object(stdClass)#85 (6) {
    ["id"]=>
    string(2) "82"
    ["name"]=>
    string(11) "googledrive"
    ["url"]=>
    string(16) "drive.google.com"
    ["shortname"]=>
    string(2) "gd"
    ["category"]=>
    string(4) "apps"
    ["img"]=>
    string(1) "y"
  }
  [36]=>
  object(stdClass)#86 (6) {
    ["id"]=>
    string(2) "84"
    ["name"]=>
    string(6) "ubuntu"
    ["url"]=>
    string(10) "ubuntu.com"
    ["shortname"]=>
    string(2) "ub"
    ["category"]=>
    string(4) "apps"
    ["img"]=>
    string(1) "y"
  }
  [37]=>
  object(stdClass)#87 (6) {
    ["id"]=>
    string(2) "89"
    ["name"]=>
    string(14) "ifthisthenthat"
    ["url"]=>
    string(9) "ifttt.com"
    ["shortname"]=>
    string(2) "if"
    ["category"]=>
    string(4) "apps"
    ["img"]=>
    string(1) "y"
  }
  [38]=>
  object(stdClass)#88 (6) {
    ["id"]=>
    string(2) "29"
    ["name"]=>
    string(6) "custom"
    ["url"]=>
    string(0) ""
    ["shortname"]=>
    string(3) "..."
    ["category"]=>
    NULL
    ["img"]=>
    string(1) "y"
  }
}

我需要按类别键对项目进行分组

4

2 回答 2

2

我不太了解您的数组结构,但这 - 希望 - 可以帮助您:

$array = array (
   '1' => array('item3'),
   '2' => array('item1', 'item2'),
   '3' => array('item4')
);

foreach($array as $id => $category) {
   echo "Category " . $id. "<br/>";
   foreach($category as $item) {
      echo $item . "<br/>";
   }
}
于 2012-10-04T21:30:24.680 回答
1

您可以创建一个数组,其中类别作为键,项目作为值:

foreach ($array as $item) {
    $itemsByCategory[$item->category][] = $item;
}

然后,您可以使用以下内容显示它们:

header('Content-Type: text/plain');
foreach ($itemsByCategory as $category => $items) {
    echo $category . "\n";
    foreach ($items as $item) {
        echo '- ' . $item . "\n";
    }
}
于 2012-10-04T22:03:22.750 回答