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?
I was doing some similar work. Didn't found any other solution, just how to set key of array element to array itself. So just created helper to set _key
to array:
array_walk($array, function(&$item, $key){
$item['_key'] = $key;
});
Then everything becomes easy:
$result = Hash::combine($array, "{n}._key", "{n}.title");
and:
$result = Hash::combine($array, "{n}._key", "{n}.extra");