I have an array:
$names = [
    "Ayush" , "Vaibhav", "Shivam",
    "Hacker", "Topper", "ABCD",
    "NameR", "Tammi", "Colgate",
    "Britney", "Bra", "Kisser"
];
And I have another variable
$addthis = "ADDTHIS";
How to make an array from these two so that after every three items in $names, the value of $addthis is added. So, I want this array as result from these two.
$result = [
    "Ayush", "Vaibhav", "Shivam", "ADDTHIS",
    "Hacker", "Topper", "ABCD", "ADDTHIS",
    "NameR", "Tammi", "Colgate", "ADDTHIS",
    "Britney", "Bra", "Kisser"
];
                "Oneliner", just for fun:
$new = array_reduce(
    array_map(
        function($i) use($addthis) { return count($i) == 3 ? array_merge($i, array($addthis)) : $i; },
        array_chunk($names, 3)
    ),
    function($r, $i) { return array_merge($r, $i); },
    array()
);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With