Different output of Set::combine and Hash::combine

Written by James McDonald

March 17, 2015

Just had an annoying 1/2 hour trying to get a properly formatted array to pass to a select control in CakePHP

In my controller I’m trying to format an array with a Custom field

 

        $options = array(
            'fields' => array(
                'ReportDate.id',
                'CONCAT( ReportDate.date, " " , Shift.name ) as report_date'
            ),
            'recursive' => 0
        );

        $report_dates = $this->Report->ReportDate->find('all', $options);

        // $this->log($reportDates);
        $reportDates = Hash::combine($report_dates, '{n}.ReportDate.id', '{n}.{n}.report_date');

 

# this is the before format (without running ::combine on it)
Array
(
    [0] => Array
        (
            [ReportDate] => Array
                (
                    [id] => 1
                )

            [0] => Array
                (
                    [report_date] => 2015-03-16 ARVO
                )

        )

    [1] => Array
        (
            [ReportDate] => Array
                (
                    [id] => 4
                )

            [0] => Array
                (
                    [report_date] => 2015-03-16 ARVO
                )

        )

    [2] => Array
        (
            [ReportDate] => Array
                (
                    [id] => 2
                )

            [0] => Array
                (
                    [report_date] => 2015-03-16 NIGHT
                )

        )

)

# this is after running   
# Set::combine($report_dates, '{n}.ReportDate.id', '{n}.{n}.report_date');
# still not right
Array
(
    [1] => Array
        (
            [0] => 2015-03-16 ARVO
        )

    [4] => Array
        (
            [0] => 2015-03-16 ARVO
        )

    [2] => Array
        (
            [0] => 2015-03-16 NIGHT
        )

)

## and after I switched to 
# Hash::combine($report_dates, '{n}.ReportDate.id', '{n}.{n}.report_date');
# notice how the two levels of {n}.{n} have properly been compressed down
Array
(
    [1] => 2015-03-16 ARVO
    [4] => 2015-03-16 ARVO
    [2] => 2015-03-16 NIGHT
)

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…