Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi 2 all
i have an array which is

PHP
<?php
$x=array(1,2,3,4,5,6);
foreach($x as $y)
{

print_r($y);
}

?> 


the above code output 123456

i want the output to be like that
(1,2)
(3,4)
(5,6)
how to that??
Posted
Comments
DinoRondelly 20-Dec-12 15:27pm    
There are lots of ways to do that what have your tried? Where are you stuck?

If you just play around with it you'll have it in no time,
TheSniper105 20-Dec-12 15:52pm    
i need to do it using foreach only
DinoRondelly 20-Dec-12 16:51pm    
Couldnt even give me 5 stars hey ...

1 solution

PHP
$x=array(1,2,3,4,5,6);
$temp = "";
foreach($x as $y)
{
if($temp !== "")
{
  print_r("(".$temp.",".$y.")");
  $temp = "";
}
else
{
  $temp = $y;
}
}


You can test here, Your welcome.

http://sandbox.onlinephpfunctions.com/[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900