How to divide CSV to two different Strings in PHP [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to divide CSV into Two String LHS_String and RHS_String. For Example, I have the below CSV -
customer^id,record^id
customer^fname,record^first_name
cusomer^gender,record^gender
The Output should be like -
LHS_String : customer^id
RHS_String : record^id

Short answer, read over http://php.net/manual/en/function.explode.php
Example:
<?php
$data = "customer^id,record^id\r\n";
$data .= "customer^fname,record^first_name\r\n";
$data .= "cusomer^gender,record^gender\r\n";
$p1 = explode("\r\n", $data);
$p2 = array();
foreach($p1 as $key => $line){
$p2[$key] = explode(",", $line);
}
echo "LHS_String : " . $p2[0][0] . "\r\n";
echo "RHS_String : " . $p2[0][1] . "\r\n";
?>

Related

How to split one group into seperate using php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want split one group AS follow
splitGroups("11133355557777") ➞ ["111", "333", "5555", "7777"]
Anyone have idea then let me know
With php version 5 above this will work link to execute
Link to an example https://paiza.io/projects/qfRZ07OP3OviWCVsUbdFtQ
function splitGroups($str){
$arr = [];
$i=0;
$sub = '';
while($i!=strlen($str))
{
$sub .= $str[$i];
if ( strlen($str)-1 == $i || $str[$i] != $str[$i+1] ){
$arr[] = $sub;
$sub='';
}
$i++;
}
return $arr;
}

How to add html on each of start and end of an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How to add html on each of start and end of an array
Here is my array = (1, 2, 3, 4, 5)
what I want to do was add <span> at the start of the array and </span> at the end so become like this
<span>1</span>, <span>2</span>,...
How I can do this
This can be done with the following,
<?php
$demo = array(1, 2, 3, 4, 5);
$string = '';
foreach ($demo as $item) {
$string .= '<span>' . $item . '</span>,';
}
echo $string;
Ends up with,
<span>1</span>,<span>2</span>,<span>3</span>,<span>4</span>,<span>5</span>,

Grouping cities by first letters in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have table on mysql like this:
insert into `city` (`ID`,`NAMES`) values
(1,'Białystok, Radom, Sandomierz'),
(2,'Olsztyn, Warka, Grójec, Poznań'),
(3,'Białowieża, Zakopane, Wrocław, Gdańsk, Sopot'),
(4,'Augustów, Kielce')
After select I would like to see a view, like:
A B C D.........Z - links to section
A
Augustów
B
Białystok
Białowieża
G
Gdańsk
Grójec
How do this in PHP?
I have tried with this PHP code.
You may help this.
$arr = array("Sky","Stackoverflow","Cloud", "Birds","Banana", "Rainbow", "Moon","Apple","Aeroplane","Ambulance");
sort($arr);
$last = '';
$concate = "<ul>";
foreach ($arr as $key => $val)
{
$first = substr($val,0,1);
if($first == $last)
{
$concate .= '<li>'.$val.'</li>';
}
else
{
$concate .= $first;
$concate .= '<li>'.$val.'</li>';
}
$last = $first;
}
$concate .= "</ul>";
echo $concate;

How To Display Random Results in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Im trying to display 2 or more unique random results from a text file, How do I do that please?
But I need unique results and not 2 or more of the same.
Currently using this code, but it only returns 1 random result:
<?php
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$randArrayIndexNum = array_rand($textArray);
$randPhrase = $textArray[$randArrayIndexNum];
?>
<?php echo $randPhrase ?>
I would use something like that
shuffle($textArray);
echo $textArray[0];
echo $textArray[1];
http://php.net/manual/tr/function.shuffle.php
You can give a shot to this also. Code is not tested. I am collecting the used into an array, and check, is that used before.
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$used = array();
$countOfRandoms = 2;
$randoms = array();
$i = 1;
do {
if ($countOfRandoms == $i) {
break;
}
$randArrayIndexNum = array_rand($textArray);
if (in_array($randArrayIndexNum, $used)) {
continue;
}
$used[] = $randArrayIndexNum;
$random = $textArray[$randArrayIndexNum];
$i++;
} while (true);

Please specify how to implement this code between echo ' '; [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have using this code between echo "";
<?php
for ($i=0; $i<$tot; $i++)
{
$rt = date ( "d-m-Y", $y[$i] ) ;
}
?>
Please specify how to implement this code between echo "";
<?php
$output = 'for ($i=0; $i<$tot; $i++){ ';
$output .= '$rt = date ( \'d-m-Y\', $y[$i] );';
$output .= '}';
echo $output;
?>
For readability I separated it into 4 lines, but you can see how it could easily be refactored into one echo statement.
The key idea here is that by declaring strings with ' instead of ", php will not interpret the $ signs as variables. Additionally, in order to include single quotes within the string itself, a backslash escape character is required as in \'d-m-Y\'

Categories