Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to merge array with custom value in laravel?

I need help to concatenate 2 arrays with custom values, I’ve used tried array_merge() and array_combine(), the result is always not same with what I want please help me guys

Array 1

$month = [
'January',
'January',
'January',
'January',
'February'
];

Array 2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$weeks = [
'Week 1',
'Week 2',
'Week 3',
'Week 4',
'Week 1'
];

I want the output be like this

$newArray = [
'January - Week 1',
'January - Week 2',
'January - Week 3',
'January - Week 4',
'February - Week 1'
];

how to get the result like that

>Solution :

There isn’t any function that will automatically give you the result you want. You need to iterate through the data and create it yourself:

$month = [
    'January',
    'January',
    'January',
    'January',
    'February'
];

$weeks = [
    'Week 1',
    'Week 2',
    'Week 3',
    'Week 4',
    'Week 1'
];

$newArray = [];

foreach ($month as $index => $value) {
    // Create the new string you want, using the same index for both arrays
    $newArray[] = $value . ' - ' . $weeks[$index];
}

Demo: https://3v4l.org/7qAoX

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading