duanjiao1256
duanjiao1256
2015-07-25 10:11

如何向多维数组添加一个维度?

已采纳

I have a multi-dimensional array that looks like this:

Array
(
  [Category-1-Name] => Array      // Category-array
  (
    [0] => Array                  // Post-array
      (
        [0] => Post-0-name
        [1] => Post-0-url
        [2] => Post-0-text
      )

    [1] => Array
      (
        [0] => Post-1-name
        [1] => Post-1-url
        [2] => Post-1-text
      )
  )

  [Category-2-Name] => Array
  (
    [0] => Array
      (
        [0] => Post-0-name
        [1] => Post-0-url
        [2] => Post-0-text
      )

    [1] => Array
      (
        [0] => Post-1-name
        [1] => Post-1-url
        [2] => Post-1-text
      )
  )
)

I fill the array like this:

$q = array();
$a = array("Post-0-name", "Post-0-url", "Post-0-text");
foreach ( $categories as $key=>$category ) {
  $b = $category->name;
}
$q[$b][] = $a;

I would like to add one more dimension: Category description for each category.

Is it posibble to do this without repeating the description in every Post-array?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doubi7739 doubi7739 6年前

    Yes, it's possible. I think the optimal way will be to create array of this kind:

    [Category-1-Name] => Array      // Category-array
    (
      [descripion] => 'some description here'
      [posts] => Array
        [0] => Array                  // Post-array
          (
            [0] => Post-0-name
            [1] => Post-0-url
            [2] => Post-0-text
          )
    
    点赞 评论 复制链接分享