【问题标题】:multidimensional array conversion is not working多维数组转换不起作用
【发布时间】:2013-06-28 13:02:33
【问题描述】:

非常沮丧的时刻,无法弄清楚,最后决定搬家并向 Geeks 询问解决方案。

好的,问题来了。

我有下面的数组,需要通过替换array中的重复记录来转换它,合并如下所示。

Array
(
    [0] => Array
    (
        [Index] => Array
        (
            [id] => 2
            [content] => content 2
            [user_id] => 1
            [page_number] => 25
            [book_id] => 1
            [tag_id] => 3,2,4
            [need_review] => no
            [status] => active
            [created] => 2013-06-27 12:36:45
            [modified] => 2013-06-27 12:36:45
        )
        [Tag] => Array
        (
            [id] => 3
            [name] => three
            [user_id] => 1
            [status] => active
            [created] => 2013-06-27 12:32:13
            [modified] => 2013-06-27 12:32:13
        )
        [Book] => Array
        (
            [id] => 1
            [name] => Book 1
            [description] => book one
            [ordervalue] => 0
            [user_id] => 1
            [status] => active
            [created] => 2013-06-27 12:22:26
            [modified] => 2013-06-27 12:22:26
        )
    )
    [1] => Array
    (
        [Index] => Array
        (
            [id] => 2
            [content] => content 2
            [user_id] => 1
            [page_number] => 25
            [book_id] => 1
            [tag_id] => 3,2,4
            [need_review] => no
            [status] => active
            [created] => 2013-06-27 12:36:45
            [modified] => 2013-06-27 12:36:45
        )
        [Tag] => Array
        (
            [id] => 2
            [name] => two
            [user_id] => 1
            [status] => active
            [created] => 2013-06-27 12:31:58
            [modified] => 2013-06-27 12:31:58
        )
        [Book] => Array
        (
            [id] => 1
            [name] => book 1
            [description] => book one
            [ordervalue] => 0
            [user_id] => 1
            [status] => active
            [created] => 2013-06-27 12:22:26
            [modified] => 2013-06-27 12:22:26
        )

    )
    [2] => Array
    (
        [Index] => Array
        (
            [id] => 1
            [content] => content1
            [user_id] => 1
            [page_number] => 10
            [book_id] => 2
            [tag_id] => 1,2,3
            [need_review] => no
            [status] => active
            [created] => 2013-06-27 12:32:17
            [modified] => 2013-06-27 12:32:17
        )
        [Tag] => Array
        (
            [id] => 3
            [name] => three
            [user_id] => 1
            [status] => active
            [created] => 2013-06-27 12:32:13
            [modified] => 2013-06-27 12:32:13
        )
        [Book] => Array
        (
            [id] => 2
            [name] => book2
            [description] => book two
            [ordervalue] => 2
            [user_id] => 1
            [status] => active
            [created] => 2013-06-27 12:24:04
            [modified] => 2013-06-27 12:24:04
        )
    )
    [3] => Array
    (
        [Index] => Array
        (
            [id] => 1
            [content] => content1
            [user_id] => 1
            [page_number] => 10
            [book_id] => 2
            [tag_id] => 1,2,3
            [need_review] => no
            [status] => active
            [created] => 2013-06-27 12:32:17
            [modified] => 2013-06-27 12:32:17
        )
        [Tag] => Array
        (
            [id] => 2
            [name] => two
            [user_id] => 1
            [status] => active
            [created] => 2013-06-27 12:31:58
            [modified] => 2013-06-27 12:31:58
        )
        [Book] => Array
        (
            [id] => 2
            [name] => book2
            [description] => book two
            [ordervalue] => 2
            [user_id] => 1
            [status] => active
            [created] => 2013-06-27 12:24:04
            [modified] => 2013-06-27 12:24:04
        )
    )
)

我想把这个数组转换成下面的。

Array
(
    [0] => Array
    (
        'Tag' => Array
        (
            [id] => 2
            [name] => two
            [user_id] => 1
            [status] => active
            [created] => 2013-06-27 12:31:58
            [modified] => 2013-06-27 12:31:58,
            [Book] => Array
            (
                [id] => 2
                [name] => book2
                [description] => book two
                [ordervalue] => 2
                [user_id] => 1
                [status] => active
                [created] => 2013-06-27 12:24:04
                [modified] => 2013-06-27 12:24:04
                [Index] => Array
                (
                    '0' => Array
                    (
                        [id] => 1
                        [content] => content1
                        [user_id] => 1
                        [page_number] => 10
                        [book_id] => 2
                        [tag_id] => 1,2,3
                        [need_review] => no
                        [status] => active
                        [created] => 2013-06-27 12:32:17
                        [modified] => 2013-06-27 12:32:17
                    ),
                    '1' => Array
                    (
                        [id] => 2
                        [content] => content 2
                        [user_id] => 1
                        [page_number] => 25
                        [book_id] => 1
                        [tag_id] => 3,2,4
                        [need_review] => no
                        [status] => active
                        [created] => 2013-06-27 12:36:45
                        [modified] => 2013-06-27 12:36:45
                    )
                )
            )
        )
    )
)

等等。

我尝试了什么?

$newindexes = array();
for( $i = 0; $i < count($indexes);$i++ )
{
    $newindexes[$i]['Book']          = $indexes[$i]['Book'];
    $newindexes[$i]['Book']['Index'] = array();

    for( $k = 0; $k < count($indexes);$k++ )
    {
        if($indexes[$i]['Book']['id'] == $indexes[$k]['Book']['id'])
        {
            $newindexes[$i]['Book']['Index'][] = $indexes[$k]['Index'];
        }
    }
}

并在末尾添加$newindexes = array_map("unserialize", array_unique(array_map("serialize", $newindexes))); 以删除重复但仍然无法正常工作。

花了将近 4-5 个小时,但还是想不通。

请大家看看我的代码并给我一些更正,或者提供一个有用的链接。

非常感谢。

【问题讨论】:

  • 你应该从写出我们的业务逻辑规则开始——即:如果两本书的索引相同,则只使用第一本书的信息。
  • @JacobS 请演示一下...
  • 嗯——所有条目是否都按顺序排列,即:书 ID 1、书 ID 1、书 ID 2、书 ID 3?除“索引”之外的所有其他信息是否相同?所有这些事情都会影响你试图解决的“问题”。

标签: php arrays multidimensional-array array-merge array-unique


【解决方案1】:

如果所有标签 ID、书籍 ID 和索引 ID 都是唯一的,我会给出一个不太可靠的答案来满足您的要求:

foreach($indexes as $index){
    $tagid = (int) $index['Tag']['id'];
    $bookid = (int) $index['Book']['id'];
    $indexid = (int) $index['Index']['id'];
    if(!isset($newindexes[$tagid])) {
        $newindexes[$tagid]['Tag'] = $index['Tag'];
    }
    if(!isset($newindexes[$tagid]['Tag']['Book']) || !isset($newindexes[$tagid]['Tag']['Book'][$bookid])) {
        $newindexes[$tagid]['Tag']['Book'][$bookid] = $index['Book'];
    }
    if(!isset($newindexes[$tagid]['Tag']['Book'][$bookid]['Index']) || !isset($newindexes[$tagid]['Tag']['Book'][$bookid]['Index'][$indexid])) {
        $newindexes[$tagid]['Tag']['Book'][$bookid]['Index'][$indexid] = $index['Index'];
    }
}

【讨论】:

    猜你喜欢
    • 2013-03-24
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    相关资源
    最近更新 更多