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

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

使用できます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

これは本当に手早く汚い方法ですが、次のようになります。

$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 に答える