非常に苛立たしい瞬間で、理解できず、最終的に移動して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時間費やしましたが、理解できませんでした。
みんな見て、コードを修正するか、役立つリンクを提供してください。
どうもありがとう。