1

当我从 mysql 数据库中检索文章信息时,我得到了一个这样的数组:

Array
(
    [0] => Array
        (
            [CategoryID] => 3
            [CategoryName] => Test Category
            [Parent] => 0
            [Thumb] => 0
            [sort] => 8
            [ArticleID] => 2
            [ArticleCatgories] => 9,4,3
            [ArticleTitle] => vxcvxcvx
            [ArticleBody] => xcvxcvxcvxcv
            [InGroup] => 0
            [GroupID] => 0
            [Published] => 1
            [PublishTime] => 
            [TermID] => 11
            [TermSlug] => xcvxcvxcv
            [RelatedID] => 2
            [TermType] => article
        )

    [1] => Array
        (
            [CategoryID] => 4
            [CategoryName] => Test Image
            [Parent] => 0
            [Thumb] => 0
            [sort] => 4
            [ArticleID] => 2
            [ArticleCatgories] => 9,4,3
            [ArticleTitle] => vxcvxcvx
            [ArticleBody] => xcvxcvxcvxcv
            [InGroup] => 0
            [GroupID] => 0
            [Published] => 1
            [PublishTime] => 
            [TermID] => 11
            [TermSlug] => xcvxcvxcv
            [RelatedID] => 2
            [TermType] => article
        )

    [2] => Array
        (
            [CategoryID] => 9
            [CategoryName] => Test Images
            [Parent] => 0
            [Thumb] => 7
            [sort] => 2
            [ArticleID] => 2
            [ArticleCatgories] => 9,4,3
            [ArticleTitle] => vxcvxcvx
            [ArticleBody] => xcvxcvxcvxcv
            [InGroup] => 0
            [GroupID] => 0
            [Published] => 1
            [PublishTime] => 
            [TermID] => 11
            [TermSlug] => xcvxcvxcv
            [RelatedID] => 2
            [TermType] => article
        )
} 

并且由于文章属于多个类别,因此它会多次检索文章信息。如果它们具有相同的 ArticleID,我需要做的是将类别名称组合到一个子数组中,这样结果就变成了:

Array
(
    [0] => Array
        (
            [CategoryName] => array('Test Category','Test Image','Test Images');
            [Parent] => 0
            [Thumb] => 0
            [sort] => 8
            [ArticleID] => 2
            [ArticleCatgories] => 9,4,3
            [ArticleTitle] => vxcvxcvx
            [ArticleBody] => xcvxcvxcvxcv
            [InGroup] => 0
            [GroupID] => 0
            [Published] => 1
            [PublishTime] => 
            [TermID] => 11
            [TermSlug] => xcvxcvxcv
            [RelatedID] => 2
            [TermType] => article
        )
}
4

3 回答 3

1

您可以使用array_reduce

$array = Array(
    "0" => Array("CategoryID" => 3,"CategoryName" => "Test Category","Parent" => 0,"Thumb" => 0,"sort" => 8,"ArticleID" => 2,"ArticleCatgories" => "9,4,3","ArticleTitle" => "vxcvxcvx","ArticleBody" => "xcvxcvxcvxcv","InGroup" => 0,"GroupID" => 0,"Published" => 1,"PublishTime" => null,"TermID" => 11,"TermSlug" => "xcvxcvxcv","RelatedID" => 2,"TermType" => "article"),
    "1" => Array("CategoryID" => 4,"CategoryName" => "Test Image","Parent" => 0,"Thumb" => 0,"sort" => 4,"ArticleID" => 3,"ArticleCatgories" => "9,4,3","ArticleTitle" => "vxcvxcvx","ArticleBody" => "xcvxcvxcvxcv","InGroup" => 0,"GroupID" => 0,"Published" => 1,"PublishTime" => null,"TermID" => 11,"TermSlug" => "xcvxcvxcv","RelatedID" => 2,"TermType" => "article"),
    "2" => Array("CategoryID" => 9,"CategoryName" => "Test Images","Parent" => 0,"Thumb" => 7,"sort" => 2,"ArticleID" => 2,"ArticleCatgories" => "9,4,3","ArticleTitle" => "vxcvxcvx","ArticleBody" => "xcvxcvxcvxcv","InGroup" => 0,"GroupID" => 0,"Published" => 1,"PublishTime" => null,"TermID" => 11,"TermSlug" => "xcvxcvxcv","RelatedID" => 2,"TermType" => "article"),
    "3" => Array("CategoryID" => 4,"CategoryName" => "Test A","Parent" => 0,"Thumb" => 0,"sort" => 4,"ArticleID" => 3,"ArticleCatgories" => "9,4,3","ArticleTitle" => "vxcvxcvx","ArticleBody" => "xcvxcvxcvxcv","InGroup" => 0,"GroupID" => 0,"Published" => 1,"PublishTime" => null,"TermID" => 11,"TermSlug" => "xcvxcvxcv","RelatedID" => 2,"TermType" => "article"),
    "4" => Array("CategoryID" => 9,"CategoryName" => "Test B","Parent" => 0,"Thumb" => 7,"sort" => 2,"ArticleID" => 2,"ArticleCatgories" => "9,4,3","ArticleTitle" => "vxcvxcvx","ArticleBody" => "xcvxcvxcvxcv","InGroup" => 0,"GroupID" => 0,"Published" => 1,"PublishTime" => null,"TermID" => 11,"TermSlug" => "xcvxcvxcv","RelatedID" => 2,"TermType" => "article")
);

$array = array_reduce($array, function ($a, $b) {
    is_null($a) ? $a = array() : null;
    if (!isset($a[$b['ArticleID']])) {
        $a[$b['ArticleID']] = $b ;
        $a[$b['ArticleID']]['CategoryName'] = array($b['CategoryName']);
    } else {
        $a[$b['ArticleID']]['CategoryName'][] = $b['CategoryName'];
    }
    return $a;
});

var_dump($array);

输出

array
  2 => 
    array
      'CategoryID' => int 3
      'CategoryName' => 
        array
          0 => string 'Test Category' (length=13)
          1 => string 'Test Images' (length=11)
          2 => string 'Test B' (length=6)
      'Parent' => int 0
      'Thumb' => int 0
      'sort' => int 8
      'ArticleID' => int 2
      'ArticleCatgories' => string '9,4,3' (length=5)
      'ArticleTitle' => string 'vxcvxcvx' (length=8)
      'ArticleBody' => string 'xcvxcvxcvxcv' (length=12)
      'InGroup' => int 0
      'GroupID' => int 0
      'Published' => int 1
      'PublishTime' => null
      'TermID' => int 11
      'TermSlug' => string 'xcvxcvxcv' (length=9)
      'RelatedID' => int 2
      'TermType' => string 'article' (length=7)
  3 => 
    array
      'CategoryID' => int 4
      'CategoryName' => 
        array
          0 => string 'Test Image' (length=10)
          1 => string 'Test A' (length=6)
      'Parent' => int 0
      'Thumb' => int 0
      'sort' => int 4
      'ArticleID' => int 3
      'ArticleCatgories' => string '9,4,3' (length=5)
      'ArticleTitle' => string 'vxcvxcvx' (length=8)
      'ArticleBody' => string 'xcvxcvxcvxcv' (length=12)
      'InGroup' => int 0
      'GroupID' => int 0
      'Published' => int 1
      'PublishTime' => null
      'TermID' => int 11
      'TermSlug' => string 'xcvxcvxcv' (length=9)
      'RelatedID' => int 2
      'TermType' => string 'article' (length=7)
于 2012-10-04T02:31:59.670 回答
1

ArticleID您可以使用以下键构建一个新数组:

$final = array();
foreach ($rows as $row) {
    $id = $row['ArticleID'];
    if (isset($final[$id])) {
        // we've already seen this article
        // append to categoryname array
        $final[$id]['CategoryName'][] = $row['CategoryName'];
    } else {
        // this is the first article occurrence
        // turn categoryname into array first
        $row['CategoryName'] = array($row['CategoryName']);
        $final[$id] = $row;
    }
}

print_r($final);

根据您的问题输出:

Array
(
    [2] => Array
        (
            [CategoryID] => 3
            [CategoryName] => Array
                (
                    [0] => Test Category
                    [1] => Test Image
                    [2] => Test Images
                )

            [Parent] => 0
            [Thumb] => 0
            [sort] => 8
            [ArticleID] => 2
            [ArticleCatgories] => 9,4,3
            [ArticleTitle] => vxcvxcvx
            [ArticleBody] => xcvxcvxcvxcv
            [InGroup] => 0
            [GroupID] => 0
            [Published] => 1
            [PublishTime] =>
            [TermID] => 11
            [TermSlug] => xcvxcvxcv
            [RelatedID] => 2
            [TermType] => article
        )

)
于 2012-10-04T02:41:11.037 回答
-1

这确实是一种快速而肮脏的方式,但这就是它的方式:

$orig是问题中提出的原始数组。

您基本上迭代数组并获取所有类别并将它们添加到$keys. 现在假设原始数组的所有其他键和值都相同(当然除了那个类别),您可以从该数组中选择任何元素并将其称为您的$final数组。

之后,您只需将类别值替换为组合类别的数组。

$orig = array();

$final = array();
$keys = array();

foreach($orig as $i)
{
    $keys[] = $i['CategoryName'];
}

$final = $orig[0];
$final['CategoryName'] = $keys;
于 2012-10-04T02:28:42.363 回答