-3

I have two arrays like this

Array1 ( 
             [0] => 10:00:00-08 
             [1] => 10:02:00-08 
             [2] => 10:03:00-08 
             [3] => 10:04:00-08 
             [4] => 10:00:00-08 
             [5] => 10:00:00-08 
             [6] => 10:00:00-08 
            )

Array2 ( 
             [0] => 19:00:00-08 
             [1] => 19:02:00-08 
             [2] => 19:03:00-08 
             [3] => 19:04:00-08 
             [4] => 19:00:00-08 
             [5] => 19:00:00-08 
             [6] => 19:00:00-08 
       )

I want The following output.

$a=10:00:00-08 - 19:00:00-08;
$b=10:02:00-08 - 19:02:00-08;
$c= 10:03:00-08 -19:03:00-08;
and so on....  
asprin
  • 9,579
  • 12
  • 66
  • 119
useraat
  • 3
  • 1

4 Answers4

2

You make use of array_combine

// this will create an array using array1 as keys and array2 as values
$combined = array_combine($array1, $array2);
foreach($combined as $key=>$value)
{
   echo $key.' - '.$value;
   echo '<br />';
}

DEMO

asprin
  • 9,579
  • 12
  • 66
  • 119
1

Try following (if they are the same size):

<?php
  for($i = 0; $i < count($array1); $i++)
  {
    echo $array1[$i] . ' - ' . $array2[$i];
  }
?>

If they are not the same size you shall change it to

<?php
  $size = min(count($array1),count($array2));

  for($i = 0; $i < $size; $i++)
  {
    echo $array1[$i] . ' - ' . $array2[$i];
  }
?>

But only the items on first common indexes will be echoed.

Edit:

If you want to assign the result to alphabet variables, try this (Variable variables):

<?php
  $size = min(count($array1),count($array2));
  $var = 'a';


  for($i = 0; $i < $size; $i++)
  {
    $$var =  $array1[$i] . ' - ' . $array2[$i];
    $var++; // Next variable name will be b, c, d, ...
  }
?>

This will assign first two values to $a and so on.

DEMO

Ladislav Gallay
  • 478
  • 6
  • 15
0
<?php 

$array1 =   array('10:00:00-08','10:02:00-08' ,'10:03:00-08' , '10:04:00-08' , '10:00:00-08' , '10:00:00-08' , '10:00:00-08' ); 
$array2 =   array ( '19:00:00-08' , '19:02:00-08' , '19:03:00-08' , '19:04:00-08' , '19:00:00-08', '19:00:00-08' , '19:00:00-08' ); 

foreach( $array1  as $key=>$value) {


    echo $value .'-'.$array2[$key]."<br/>" ;


    }


?>

output will be

10:00:00-08-19:00:00-08 10:02:00-08-19:02:00-08 10:03:00-08-19:03:00-08 10:04:00-08-19:04:00-08 10:00:00-08-19:00:00-08 10:00:00-08-19:00:00-08 10:00:00-08-19:00:00-08

Rohit Goel
  • 3,396
  • 8
  • 56
  • 107
0

The problem with this is, you assume that you only have a maximum of 26 combinations (a-z), so I would recommend using a different approach and putting the output into an array rather. Also, you wont have any idea if $k is set for example.

If you really want to put them into variables like $a, $b, $c you could do something like this, but like I said, the approach you are using is not ideal:

$input_a = array("10:00:00-08");
$input_b = array("19:00:00-08");
$alphabet = range('a','z');
foreach ($input_a as $index=>$val) {
    ${$alphabet[$index]} = $val . "-" . $input_b[$index];
}
  • But you can iterate. If you have `$var = 'z'` and you do `$var++` the next value is `aa`. See http://stackoverflow.com/questions/2673360/most-efficient-way-to-get-next-letter-in-the-alphabet-using-php – Ladislav Gallay May 27 '14 at 08:14
  • 1
    Cool. Didnt know that :-) Still though, you wouldn't know if $aa even exists, so this method is a little fail in my opinion. Better to store it in another array: $output[$var++] = $val . "-" . $input_$b[$index]; – user3675480 May 27 '14 at 08:24