组合子数组中的数组值?

时间:2012-10-04 02:20:44

标签: php arrays multidimensional-array

当我从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
        )
}

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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
        )

)

答案 2 :(得分:0)

这实际上是一种快速而肮脏的方式,但事实就是这样:

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

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

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

$orig = array();

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

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

$final = $orig[0];
$final['CategoryName'] = $keys;