I have values in my database that has values with commas. Eg:
Monday,Tuesday,Wednesday,Thursday,Friday. When I call the row from the database where certain id, I want to write the the result to a text file after every comma into a new line as follows:
Monday
Tuesday
Wednesday
Thursday
Friday
One below the other.
Hope this makes sense.
You can use explode function.
explode — Split a string by string
$days = "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday";
$weekdays = explode(",", $days);
foreach( $weekdays as $value ){
echo $value."\n";
}
Output:
Monday
Tuesday
Wednesday
Thursday
Friday
I think it will work for you!
Something like that:
$result = "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday";
$array = explode(",", $result);
foreach( $array as $key => $value ){
echo $value."\n";
}
explode function will help you
$str = "Monday,Tuesday,Wednesday,Thursday,Friday";
$val = explode(",",$str);
foreach ($val as $key=>$value) {
echo $value.'</br>';
}
Use something like this
$colVal = $row['col-name'];
echo str_replace(",", "\r\n", $colVal);
Related
I want to insert a new line after n commas.
For example I got this value: 385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426
How I could echo them all, but every 5th comma there should be a linebreak?
385,386,387,388,389,
390,391,392,393,394,
395,396,397,398,399,
400,401,402,403,404,
405,406,407,408,409,
410,411,412,413,414,
415,416,417,418,419,
420,421,422,423,424,
425,426
Here's one method:
// Get all numbers
$numbers = explode(',', $str);
// Split into groups of 5 (n)
$lines = array_chunk($numbers, 5);
// Format each line as comma delimited
$formattedLines = array_map(function ($row) { return implode(',', $row); }, $lines);
// Format groups into new lines with commas at the end of each line (except the last)
$output = implode(",\n", $formattedLines);
Try this
<?php
//Start //Add this code if your values in string like that
$string = "385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426";
$string_array = explode(',', $string);
//End //Add this code if your values in string like that
//If you have values in array then direct use below code skip above code and replace $string_array variable with yours
end($string_array);
$last = key($string_array);
foreach ($string_array as $key => $value) {
if($last==$key){
echo $value;
}else{
echo $value.',';
}
if(($key+1)%5==0){
echo "<br />";
}
}
?>
Try like this.
You can explode the string with commas and check for every 5th
position there should be a line break.
You can check it with dividing key with 5.(i.e) it will give you a
remainder of 0
Please note that key starts from 0, so I have added (key+1), to make it start from 1
$string = "385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426";
$stringexplode = explode(",", $string);
$countstringexplode = count($stringexplode);
foreach($stringexplode as $key => $val)
{
$keyIncrement = $key+1;
echo $val.($countstringexplode == $keyIncrement ? "" : ",") ;
if(($keyIncrement) % 5 == 0)
echo "<br>";
}
?>
So i have a very simple snippet. I studied the in_array command and found it would be what i need.
However its not working?
I have tried several scenarios:
$this->item->tour_days is an array containing 1,2,3
test 1
$days = $this->item->tour_days;
$array = array($days);
if (in_array(2,$array,TRUE)) {
echo 'Tuesday';
}
test 2
$days = $this->item->tour_days;
$array = array($days);
if (in_array(2,$array)) {
echo 'Tuesday';
}
test 3
$days = $this->item->tour_days;
$array = array($days);
if (in_array('2',$array)) {
echo 'Tuesday';
}
I have tried to echo Tuesday where Tuesday = 2 from my csv but no luck.
Thanks in advance for nay help here
jonny
To convert a string containing a comma-delimited list into an array, use explode:
$array = explode(',', $days);
$this->item->tour_days is an array containing 1,2,3
Then why do you want to make another array out of it?
remove the $array = array($days); converstion.
Instead say $array = $days;
Try this:
$array = explode(",",$days);
This should work for you:
If $days is a string then use this:
$days = "1,2,3"; //$this->item->tour_days;
$array = explode(',', $days);
if(in_array(2, $array))
echo "Tuesday";
?>
If $days is already an array use this:
<?php
$days = array(1,2,3); //$this->item->tour_days;
if(in_array(2, $days))
echo "Tuesday";
?>
I have a lengthy text field containing multiple date and time separated by comma. Following is the example
2013-08-26 10:00:00,2013-08-26 15:00:00,2013-08-26 20:00:00
2013-08-27 01:00:00,2013-08-27 06:00:00,2013-08-27 11:00:00
2013-08-27 16:00:00,2013-08-27 21:00:00,2013-08-28 02:00:00
2013-08-28 07:00:00,2013-08-28 12:00:00,2013-08-28 17:00:00
From the above example I need the following structure
Step 1: Split comma separated values into one array.
Step 2: From the above obtained array push all 2013-08-26 dates to one array, the 2013-08-27 to another array and then 2013-08-28.
I successfully exploded the main text to one array but did not succeed to obtain list of values of one particular date.
foreach (explode(PHP_EOL, $dates) as $line)
{
foreach (explode(',', $line) as $dt)
{
$dateVal = date('Y-m-d', strtotime($dt));
switch ($dateVal)
{
case '2013-08-26':
$arr1[] = $dt;
break;
case '2013-08-27':
$arr2[] = $dt;
break;
case '2013-08-28':
$arr3[] = $dt;
break;
}
}
}
Online demo
$dates = "2013-08-26 10:00:00,2013-08-26 15:00:00,2013-08-26 20:00:00
2013-08-27 01:00:00,2013-08-27 06:00:00,2013-08-27 11:00:00
2013-08-27 16:00:00,2013-08-27 21:00:00,2013-08-28 02:00:00
2013-08-28 07:00:00,2013-08-28 12:00:00,2013-08-28 17:00:00";
$dates = explode("\n", $dates);
foreach($dates as $date) {
$date = explode(",", $date);
var_dump($date);
// i.e. with $date[1] I will get the middle date of particular line.
}
$date inside foreach, as you can see becomes an array (after exploding) containing all dates separated by commas in one line.
it's been a while since the last time I used PHP, but this is my rusty solution:
$map = array();
$records = explode(',', $input);
foreach( $records as $record )
{
$a = explode(" ", $record);
if(!array_key_exists( $a[0], $map ))
$map[$a[0]] = array();
$map[$a[0]][] = $a[1];
}
var_dump($map);
I haven't tested it, sorry. But it might work...
it should give you something like:
array(2)
(
['2014-03-14'] => array(2)
(
[0] => '04:03:24'
[1] => '04:03:25'
)
['2014-03-14'] => array(2)
(
[0] => '04:03:24'
[1] => '04:03:25'
)
)
I am collecting html text area data to echo in php.I am able to select all data using
$devices = explode("\n", $_POST['devs']);
foreach($devices as $device)
echo $device;
and I am able to select only the first line using:
$first_line = strstr(($_POST['devs']), "\n", true);
echo $first_line;
But How can I echo specific lines ? say line 2 or 4 from text area ?
Usage:
getLines(YOUR POST, START LINE, END LINE(optional));
With return array:
function getLines($text, $start, $end = false)
{
$devices = explode("\n", $text);
$append = "My device is ";
$output = array();
foreach ($devices as $key => $line)
{
if ($key+1 < $start) continue;
if ($end && $key+1 > $end) break;
$output[] = $append.$line;
}
return $output;
}
$array = getLines($_POST['devs'], 2);
var_dump($array);
With echo string:
function getLines($text, $start, $end = false)
{
$devices = explode("\n", $text);
$append = "My device is ";
$output = "";
foreach ($devices as $key => $line)
{
if ($key+1 < $start) continue;
if ($end && $key+1 > $end) break;
$output .= $append.$line."<br />";
}
return $output;
}
echo getLines($_POST['devs'], 2);
Your first code snippet is already creating an array of lines via the explode function.
As such, to output the 2nd and 4th lines, you can simply use:
$devices = explode("\n", $_POST['devs']);
echo $devices[1];
echo $devices[3];
If you're new to PHP (I'm guessing this is the case due to the nature of your question), it should be noted that like many programming languages, arrays are indexed from zero, hence line 2 is 1, line 4 is [3], etc.
UPDATE
To access the penultimate (i.e.: 2nd to last) line, you could use:
echo $devices[count($devices) - 2];
What we're doing here is getting the number of elements in the array (via count) and then subtracting two to fetch the second last element. (As we need to subtract one to deal with the fact that arrays are indexed from zero.)
Do it like this
$nth_line = explode("\n", $_POST['devs'])[n];
where n is you line no.
the explode() returns an array then you can select each element by basic array operation
further readings http://php.net/manual/en/function.explode.php
because $devices is an array after exploding it, you can treat each line by it's index. Reminder that arrays are zero-index based so 1 starts at 0.
$devices = explode('\n', $_POST['devs']);
// line 1
echo $devices[0];
// line 2
echo $devices[1];
// line 4
echo $devices[3];
you can use split:
$lines = split("\n", $_POST['devs']);
echo $lines[3]; //4th line
See documentation http://php.net/manual/es/function.split.php
Take a look at array operations in PHP. Since $devices is an array you can select an element by its index like this: $devices[1] for second element, $devices[2] for third etc.
Lookup your syntax on php.net. It is
$devices = explode(";", "aap;noot;mies");
print_r($devices);
foreach ($devices as $key => $value) {
echo "<br>nr.$key=" . $devices[$key];
}
OK, this is weird. I have an array of time blocks:
$time_block = array("09:00:00-13:00:00","10:00:00-14:00:00");
And I want to loop through them to create a start time and end time for each block:
foreach($time_block as $val)
{
for($x = 0; $x < count($time_block); $x++)
{
$time=$val[$x];
echo substr($time,0,8);
echo "<br>";
echo substr($time,-8);
}
echo "<br>";
echo "<br>";
}
The end result should be:
09:00:00
13:00:00
10:00:00
14:00:00
But I am getting:
0
09
9
1
10
0
Any thoughts?
You wrote: $time = $val[$x];
$val is not an array, it's a string, and seems like you are doing a foreach and then a for on the same array?
You can use something like:
foreach ($timeblock as $timeString) {
list($start, $end) = explode("-", $timeString);
echo "$start $end\n";
}
foreach($time_block as $v) {
print_r(explode( '-',$v ));
echo '<br><br>';
}
In your foreach loop, $val is a string. When you ask for $val[$x], your asking for the xth character of that string. Probably not what you want.
Solution:
Take out the for statement, and change
$time=$val[$x];
to
$time=$val;
Try this:
for($x = 0; $x < count($time_block); $x++){
$time = $time_block[$x];
echo substr($time,0,8);
echo "<br>";
echo substr($time,-8);
echo "<br><br>";
}
You're using 2 nested loops while you only have 1 dimension in your data set. That should make you think.
The problem with your approach is you're trying to loop through the element two times. If you use foreach($time_block as $val), in every iteration $val will be a single element of the array. For example, on the first run, it will store "09:00:00-13:00:00". So in your for loop, you were trying to loop through this $val, which is actually possible as strings can be accessed as arrays (the characters in the string being the elements).
This will produce the exact output you needed:
foreach($time_block as $val) {
$t=explode('-', $val);
echo $t[0]."<br>".$t[1]."<br><br>";
}
Here, explode() is used to separate the time values in $val based on the - character. $t will be an array, holding the two times in $val as its two elements.
In your code you are double looping, just use it like this:
$time_block = array("09:00:00-13:00:00","10:00:00-14:00:00");
foreach($time_block as $time) {
echo substr($time,0,8) . " ";
echo substr($time,-8) . "\n";
}
OUTPUT
09:00:00 13:00:00
10:00:00 14:00:00