多维数组转换不工作 [英] multidimensional array conversion is not working
问题描述
非常令人沮丧的时刻,无法弄清楚,最后决定移动和要求怪才有关的解决方案。
Very frustrating moment and could not figure out and finally decided to move and ask to Geeks about solution.
好吧,这是问题所在。
下面我有数组,需要从阵列代替重复的记录,将其转换
键,如下图所示合并。
I have below array and need to convert it by replacing repeating records from array
and merge is as shown below.
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
)
)
)
我想这个数组转换成以下。
I want to convert this array into below.
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(反序列化,array_unique(array_map(连载,$ newindexes)));
末去除重复,但还是没有工作。
and added $newindexes = array_map("unserialize", array_unique(array_map("serialize", $newindexes)));
at the end for removing duplicates but still not working.
几乎花了4-5个小时,但无法弄清楚。
Spent almost 4-5 hours but could not figure it out.
请你们看看,并给我一些校正我的code或给我提供一个有用的链接。
Please guys take a look and give me some correction in my code or provide me a helpful link.
非常感谢。
推荐答案
下面是一个不太可靠的答案,我会给你问什么,但前提是所有的标签ID,书籍ID和索引ID是唯一的:
Here is a less robust answer that I would give to do what you ask, provided that all tag ids, book ids, and index ids are unique:
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'];
}
}
这篇关于多维数组转换不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!