对不起,我的英语不好 :(
我想用 cakephp(或 php)将“平面数组”转换为“嵌套数组”
由此 :
$results = array(
array('customer' => 'John', 'hotel' => 'Sheraton', 'roomtype' => 'Single'),
array('customer' => 'John', 'hotel' => 'Sheraton', 'roomtype' => 'Double'),
array('customer' => 'John', 'hotel' => 'Sheraton', 'roomtype' => 'Triple'),
array('customer' => 'John', 'hotel' => 'Hilton', 'roomtype' => 'Single'),
array('customer' => 'Doe', 'hotel' => 'Hilton', 'roomtype' => 'Single'),
array('customer' => 'Doe', 'hotel' => 'Hilton', 'roomtype' => 'Double'),
array('customer' => 'Doe', 'hotel' => 'Sheraton', 'roomtype' => 'Single')
);
进入这个:
$results = array(
array(
'customer' => 'Jhon',
'children' => array(
array(
'hotel' => 'Sheraton',
'children' => array(
array('roomtype' => 'Single'),
array('roomtype' => 'Double'),
array('roomtype' => 'Triple')
)
),
array(
'hotel' => 'Hilton',
'children' => array(
array('roomtype' => 'Single')
)
),
)
),
array(
'customer' => 'Doe',
'children' => array(
array(
'hotel' => 'Hilton',
'children' => array(
array('roomtype' => 'Single'),
array('roomtype' => 'Double')
)
),
array(
'hotel' => 'Sheraton',
'children' => array(
array('roomtype' => 'Single')
)
),
)
)
);
我尝试了循环(for,foreach,while)
我也尝试了 Set::nest() cakephp 实用程序,但我没有找到解决方案:(
有什么“神奇的解决方案”可以解决这个问题吗?
谢谢