Jump to content

comma separated data in db - > array


liquidskin

Recommended Posts

I need to loop through my db and create strings from 2 columns on my db.

 

data in column1 is '22,33,33,33,25,25,25,14,14,14,14,14,14'

data in column2 is '11am,12pm,1pm,2pm,3pm,4pm,5pm,6pm,7pm,8pm,9pm,10pm, 11pm'

 

the end result I need is a string such as:

 

$body= "11am, 22 <line break> 12pm, 33 <line break> ......

 

If anyone has any examples of something like this or can get me started it would be much appreciated.

Link to comment
Share on other sites

I feel like I'm getting close...

 

//   separate array values by comma and store
   for ( $i = 0; $i < $rowcount; $i += 1) {


$string1 = $precipData[$i];
$string2 = $hourData[$i];
$explode1[$i] = explode(",", $string1);  //precip
$explode2[$i] = explode(",", $string2);  //hours

$countHolder = $explode1[$i];

$countValuesInArray = count($countHolder);

// loop through # of values in array
 for ( $j = 0; $j < $countValuesInArray; $j += 1) {
$bodyBuild[$j] = $explode2[$i][$j]. "\n". $explode1[$i][$j];
}

}



$comma_separated = implode(",", $bodyBuild);
echo $comma_separated; 

 

this outputs " 9pm 40,10pm 40,11pm 40,12am 40,1am 40,2am 40,3am 40,4am 40,5am 40,6am 40,7am 40,8am 20,9am 20"

 

which is the last row in the db.  I need to store this for each row, plus I need the linebreak.

Link to comment
Share on other sites

Something like this should work:

<?php
$col1 = explode(',','22,33,33,33,25,25,25,14,14,14,14,14,14');
$col2 = explode(',','11am,12pm,1pm,2pm,3pm,4pm,5pm,6pm,7pm,8pm,9pm,10pm,11pm');
$tmp = array();
foreach ($col2 as $i=>$v) {
     $tmp[] = $v . ',' .  $col1[$i];
}
$body = implode("<br>\n",$tmp) . "<br>\n";
echo $body;
?>

 

Ken

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.