-3

i want to implode multiple array in php For Example

$day[] = array (1,2,3,4,5);
$month[] = array (1,2,3,4,5);
$year[] = array (2001,2002,2003,2004,2005);
$date = implode(";",$day."/".$month."/".$year);

I am expecting the output is

1/1/2001;2/2/2002;3/3/2003;4/4/2004;5/5/2005

Is this possible , Actually tried and its not working. can u help me to solve this problem.

Dinesh G
  • 1,325
  • 4
  • 17
  • 24
  • It's called a loop, welcome to programming. But your code is faulty to begin with as you're creating multidimensional arrays. Read the [implode](http://php.net/implode) manual to see what arguments it accepts. – Devon Bessemer Oct 17 '15 at 07:02

2 Answers2

3

The code below creates an array with the inner format you want and then implodes it. Note, that this code assumes that the element number of each array is equal. Also, make sure that the values will not have separators, such as / or ;.

$day[] = array (1,2,3,4,5);
$month[] = array (1,2,3,4,5);
$year[] = array (2001,2002,2003,2004,2005);
$arr = array();
for ($index = 0; $index < count($day); $index++) {
    $arr[$index] = $month[$index]."/".$day[$index]."/".$year[$index];
}
$result = implode(";", $arr);
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
2

try this

$dates = array();
foreach ($day as $key => $val) {
  $dates[] = $day[$key]."/".$month[$key]."/".$year[$key];
}
$allDates = implode(";",$dates);
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
  • Syed, this way you are only echoing the result. You will have a ; at its end, which clearly differs from the format the op specified. Also, the op might want to use the result elsewhere as well, but your code assumes that the result will be only echoed. That assumption is premature, since the op does not have an echo in his code. – Lajos Arpad Oct 17 '15 at 07:15