用cakephp(或php)将'flat array'转换为'嵌套数组'

时间:2021-08-16 21:32:53

sorry for my bad english :(

对不起,我的英语不好 :(

i want to convert a 'flat array' into 'nested array' with cakephp(or php)

我想用cakephp(或php)将'flat array'转换为'嵌套数组'

from this :

由此 :

$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')
);

into this:

进入这个:

$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')
                )
            ),
        )
    )
);

i tried the loops(for, foreach, while)

我尝试了循环(for,foreach,while)

i tried also the Set::nest() cakephp utility but i don't figure out a solution :(

我也尝试了Set :: nest()cakephp实用程序,但我没有找到解决方案:(

is there any "magic solution" to resolve this ?

是否有任何“神奇的解决方案”来解决这个问题?

thanks

谢谢

1 个解决方案

#1


1  

Try this

尝试这个

    $convert = array();
    $customer_key = array();
    $hotel_key = array();
    $index = 0;
    foreach ($results as $row) {
        if (!isset($customer_key[$row['customer']])) {
            $customer_key[$row['customer']] = $index;
            $index++;
        }
        $key = $customer_key[$row['customer']];
        $convert[$key]['customer'] = $row['customer'];
        if (!isset($hotel_key[$key])) {
            $hotel_key[$key][$row['hotel']] = 0;
        }
        elseif (!isset($hotel_key[$key][$row['hotel']])) {
            $hotel_key[$key][$row['hotel']] = count($hotel_key[$key]);
        }
        $h_key = $hotel_key[$key][$row['hotel']];
        $convert[$key]['children'][$h_key]['hotel'] = $row['hotel'];
        $convert[$key]['children'][$h_key]['children'][]= array('roomtype' => $row['roomtype']);
    }

    print_r($convert);

It should convert your given array to accepted array

它应该将您给定的数组转换为接受的数组

#1


1  

Try this

尝试这个

    $convert = array();
    $customer_key = array();
    $hotel_key = array();
    $index = 0;
    foreach ($results as $row) {
        if (!isset($customer_key[$row['customer']])) {
            $customer_key[$row['customer']] = $index;
            $index++;
        }
        $key = $customer_key[$row['customer']];
        $convert[$key]['customer'] = $row['customer'];
        if (!isset($hotel_key[$key])) {
            $hotel_key[$key][$row['hotel']] = 0;
        }
        elseif (!isset($hotel_key[$key][$row['hotel']])) {
            $hotel_key[$key][$row['hotel']] = count($hotel_key[$key]);
        }
        $h_key = $hotel_key[$key][$row['hotel']];
        $convert[$key]['children'][$h_key]['hotel'] = $row['hotel'];
        $convert[$key]['children'][$h_key]['children'][]= array('roomtype' => $row['roomtype']);
    }

    print_r($convert);

It should convert your given array to accepted array

它应该将您给定的数组转换为接受的数组