jeudi 21 juillet 2016

Set::classicExtract() CakePHP 2.x Hash:: alternative

I have an array like this:

$array = array(
    1234 => array(
        'title' => 'TitleA',
        'extra' => array(
            45 => 'SubA',
            54 => 'SubB',
        ),
        // More irrelevant data
    ),
    2345 => array(
        'title' => 'TitleB',
        'extra' => array(
            89 => 'SubC',
            98 => 'SubD',
        ),
        // More irrelevant data
    ),
);

I use:

$data = Set::classicExtract($array, '{d+}.title');

To get an array like:

array(1234 => 'TitleA', 2345 => 'TitleB');

And:

$data = Set::classicExtract($array, '{d+}.extra');

To get an array like:

array(
    1234 => array(
        45 => 'SubA',
        54 => 'SubB',
    ),
    2345 => array(
        89 => 'SubC',
        98 => 'SubD',
    ),
)

Now that the Set:: class has been deprecated for a while (since 2.2, we use 2.8) I would like to use the Hash:: class. But I can not find a function that will give me the same result. I've tried both Hash::extract() and Hash::combine() but without success.

Is this even possible with the Hash:: class?

Aucun commentaire:

Enregistrer un commentaire