1

I have the following array

$array['projects'] = [
    'name1' => [
        'task' => [
            'tags' => ['value1', 'value2'],
            'email' => 'email2',
            'description' => 'mpla'
        ],
        'email' => 'email1',
        'tags' => ['value1', 'value3'],
        'title' => 'mpla'
    ]
];

Is there anyway I could use the Hash class of CakePHP 3 or maybe another class of CakePHP framework to achieve the following result:

$array['projects'] = [
    'name1' => [
        'email' => 'email2',
        'tags' => ['value1', 'value2'],
        'title' => 'mpla'
        'desciption' => 'mpla'
    ]
];

If you also know anyother package that it can handle arrays and get my job done it will do.

1 Answer 1

2

Not sure that this can be easily achieved using Cake's Hash utility. You can easily extract the array items indexed by task using combine(), but not sure how you would then go about extracting the title values and combining those with the other array elements using Hash:-

Hash::combine($array, 'projects.{s}', 'projects.{s}.task');

Perhaps the simplest solution is to use a foreach loop like this:-

$data = [];
foreach ($array['projects'] as $value) {
    $data['projects'] = $value['task'] + ['title' => $value['title']];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Basically my array is way deeper than the example I wrote and foreach is not so preferable option.
I think what you are asking for is beyond the scope of the Hash utility. Hash actually uses for loops to build up the altered arrays anyway, so I suspect foreach loops are your best option. If your actual problem is more complex than what you've posted you can perhaps take a combined approach of using loops and Hash methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.