合并数组的问题

时间:2015-01-04 12:24:55

标签: php

我有很多像下面那样的数组。这些是从数据库中检索的数据的结果。我需要最后一个数组来提供所提到的结构,因为我需要预先循环每个'注释'在连续的代码中。我不能只使用for循环。

    array('title'   => 'Testing',
          'content' => 'some text',
          'created' => 'time',
          'tags'    => array(array('title' => 'hello')),
    )
    array('title'   => 'Testing',
          'content' => 'some text',
          'created' => 'time',
          'tags'    => array(array('title' => 'hello')),
    )

我想以格式数组合并所有这些:

array(
    array('notes' =>
        array('title'   => 'Testing',
              'content' => 'some text',
              'created' => 'time',
              'tags'    => array(array('title' => 'hello')),
        ),
    ),
    array('notes' =>
        array('title'   => 'Testing',
              'content' => 'some text',
              'created' => 'time',
              'tags'    => array(array('title' => 'hello')),
        ),
    ),
);

我尝试过很多PHP函数,但没有人给我我想要的结果。有谁可以帮助我吗?是否有内置的PHP函数可以做到这一点?有人能告诉我一个例子。

感谢。

3 个答案:

答案 0 :(得分:2)

如果我正确理解您的问题,您实际要做的就是将每个数组元素放入另一个数组notes并且值为元素的数组(另一个数组)。

在这种情况下,使用foreacharray_map即可。

$newArray = array_map(function($v) { return ['notes' => $v]; }, $array);

或使用foreach(更冗长的方式)......

foreach($array as $value) {
    $newArray[] = ['notes' => $value];
}

答案 1 :(得分:1)

这是评论我想要格式化...

你问过一个阵列' '阵列条目'每个条目的位置

  • 是一个数组
  • 每个数组都有一个键注释'和相关的数据阵列。

我认为'标签'如果没有额外的“嵌套”级别,您的部分结构将更容易使用。

以下是创建和显示“想要”的代码。输出带有'标记'条目改变了:

$wanted = array(
            array('notes' =>
                array('title'   => 'Testing 1',
                      'content' => 'some text 1',
                      'created' => date('Y-m-d H:i:s'),
                      'tags'    => array('title' => 'hello',
                                          'tagFoo' => 'foo' ),
                ),
            ),
            array('notes' =>
                array('title'   => 'Testing 2',
                      'content' => 'some text 2',
                      'created' => date('Y-m-d H:i:s'),
                      'tags'    => array('title' => 'hello',
                                          'tagBar' => 'bar'),
                ),
            ),
        );

echo '<pre>';
print_r($wanted);
echo '</pre>';

这是输出:

Array
(
    [0] => Array
        (
            [notes] => Array
                (
                    [title] => Testing 1
                    [content] => some text 1
                    [created] => 2015-01-04 15:39:34
                    [tags] => Array
                        (
                            [title] => hello
                            [tagFoo] => foo
                        )
                )
        )

    [1] => Array
        (
            [notes] => Array
                (
                    [title] => Testing 2
                    [content] => some text 2
                    [created] => 2015-01-04 15:39:34
                    [tags] => Array
                        (
                            [title] => hello
                            [tagBar] => bar
                        )
                )
        )
)

答案 2 :(得分:0)

一种可能性是:

foreach($tags as $key => $note){
    $note["created"] = $created[$key]; //if created is an array
    $note["created"] = $created; //if created is a string
}
相关问题