conan
conan

Reputation: 1337

How to combine three arrays into foreach and then use the string to create another array in php?

Look at my code below, first, I want to combine three arrays together to create content in the foreach loop, and then create the final array by using the first array and the content string inside the foreach. It is kind of challenge for me, help, appreciate.

<?php
//above code, I deleted them, unnecessary to show  
 $fruit = explode(',',$fruit);
 $type = explode(',',$type);
 $date = explode(',',$date);

foreach (array_combine($fruit, $type, $date) as $fruit => $type => $date) {
     echo $content = $fruit.'is'.$type.'at'.$date;
}

//create my final array
  $total = array(
                  'date'=>$date, 
                  'content'=>$content
              );
?>

Upvotes: 1

Views: 41

Answers (1)

Ormoz
Ormoz

Reputation: 3013

Please try:

<?php
   $fruits = explode(',',$fruit);
   $types = explode(',',$type);
   $dates = explode(',',$date);
   $total=array();
   foreach ($fruits as $index=>$fruit) {
     $type=$types[$index];
     $date=$dates[$index];
     echo $content = $fruit.'is'.$type.'at'.$date;
     $total[]=array('fruit'=>$fruit,'date'=>$date,'type'=>$type,'content'=>$content);
   }
?>

Upvotes: 2

Related Questions