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

时间:2015-07-25 10:11:47

标签: php multidimensional-array

我有一个看起来像这样的多维数组:

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
      )
  )
)

我像这样填充数组:

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

我想为每个类别添加一个维度:类别说明

是否可以在不重复每个Post-array中的描述的情况下执行此操作?

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。我认为最佳方法是创建这种阵列:

skypjack