Related
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 have probably information about whether it is available in PHP to detect a duplicate and a book about not removing it and adding it to -1, -2, -3
Example:
$string = 'H4,H4,H3,WY21W,W5W,W5W,WY21W,W21/5W,W21/5W,W21W,W16W,W5W';
Result:
$output = 'H4,H4-1,H3,WY21W,W5W,W5W-1,WY21W-1,W21/5W,W21/5W-1,W21W,W16W,W5W-2'
$string = 'H4,H4,H3,WY21W,W5W,W5W,WY21W,W21/5W,W21/5W,W21W,W16W,W5W';
$parts = explode(',', $string); // split by comma
$used = []; // array to count the number of occurrences
$result = []; // array to take the "new" elements
foreach($parts as $part) {
if(isset($used[$part])) { // if there is an entry in the counter array already,
// increment it by one,
$used[$part]++;
}
else { // else initialize with 0
$used[$part] = 0;
}
// put part into new array, append -(counter) if counter > 0
$result[] = $part . ($used[$part] ? '-'.$used[$part] : '');
}
echo implode(',', $result); // join together with comma
Pretty much the same as misorude's answer.
<?php
$string = 'H4,H4,H3,WY21W,W5W,W5W,WY21W,W21/5W,W21/5W,W21W,W16W,W5W';
$values = explode(',', $string);
foreach($values as $k => $value)
if($counts[$value] = !isset($counts[$value]) ? 0 : $counts[$value]-1)
$values[$k] = $value . $counts[$value];
print implode(',', $values);
Output:
H4,H4-1,H3,WY21W,W5W,W5W-1,WY21W-1,W21/5W,W21/5W-1,W21W,W16W,W5W-2
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 6 years ago.
Improve this question
I am facing a problem.
My table contain a field named 'tags'.
This field have comma separated values.
For ex: Hello, start, good & another one is: Time, Goal, Lucky, start.
Now my problem is that, I want to send each comma separated values in json array
and none of them should repeat itself.
My array should be like this:
{
"tag":[
"Hello",
"start",
"good",
"Time",
"Goal",
"Lucky"
]
}
Just do this with your code.
Assuming $tag_string has the comma separated data.
<?php
$tag_string= "Hello, start, good, Time, Goal, Lucky, start";
$val['tag'] = explode(",", $tag_string);
$val['tag'] = array_unique($val['tag']);
echo json_encode($val);
?>
As you asked in comment answer::
<?php
$tag_string = "Hello, start, good, Time, Goal, Lucky, start";
$val = explode(",", $tag_string);
$val_count = count($val);$html = "[ ";$counter = 1;$data_validate = [];
foreach ($val as $val_row)
{
if (!in_array($val_row, $data_validate))
{
$data_validate[] = $val_row;
$html .= '{"tag":"' . $val_row . '"}';
$html .= $counter < $val_count-1 ? ',' : '';
}
$counter ++;
}
$html .= " ]";
echo $html;
?>
Use a regexp to make à php array then just use jdon_encode() on your array.
You can also use php explose() function to make your php array.
use json_encode(explode(', ', "Hello", "start", "good", "Time", "Goal", "Lucky"));
if you don't have space in your string.
split String using delimiter "," using explode() and remove duplicate value array_unique.
$your_str = "Hello,start,good,Time,Goal,Lucky"
$res["tag"] = array_unique(explode(",", $your_str));
print_r(json_encode($res));
<?php
$tag_string= "Hello, start, good, Time, Goal, Lucky, start";
$result = [];
$result['tag'] = array_unique(preg_split('/\s*,\s*/', $tag_string, NULL, PREG_SPLIT_NO_EMPTY));
echo json_encode($result);
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 6 years ago.
Improve this question
I have an array with comma separated numbers and I want to remove all duplicated numbers. Is the approach below the best choice or is there a smarter one?
$filter_tags_array[] = '4,6,2,5,8';
$filter_tags_array[] = '6,8,1,3,5,7,2,4';
$filter_tags_array[] = '2,4,1,3,5';
$filter_tags_array[] = '6,3,5,8,2,4';
$filter_tags_array[] = '2,8,11,4,9,12';
$filter_tags_array[] = '9,11,2,8,10,12,4';
$tags_id = '';
foreach($filter_tags_array as $filter_tags) {
$tags_id .= $filter_tags . ',';
}
$tags_id = implode(',', array_unique(explode(',', $tags_id)));
echo "<pre>";
print_r($tags_id);
print_r results: 4,6,2,5,8,1,3,7,11,9,12,10,
It would be better to implode the array rather than concatenate each string. This is because php has to create a new string every time.
https://3v4l.org/fUEtn
This should work for you, see here
$filter_tags_array[] = '4,6,2,5,8';
$filter_tags_array[] = '6,8,1,3,5,7,2,4';
$filter_tags_array[] = '2,4,1,3,5';
$filter_tags_array[] = '6,3,5,8,2,4';
$filter_tags_array[] = '2,8,11,4,9,12';
$filter_tags_array[] = '9,11,2,8,10,12,4';
$tags_id = implode(',',array_unique(explode(',', (implode(',', $filter_tags_array)))));
print($tags_id);
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 7 years ago.
Improve this question
I want to retrieve comma separated values from a database and store each value in a variable.
For example: in my column I have the following: 2,3,5,8,9,21
I'd like to store the numbers inside variables like this:
$a=2
$b=3
$c=5
// and so on…
If you wanna load the string that contain integers separated with ',' or '.' or 'a' or ';' do:
If you wanna go for ',':
$char = ',';
If you wanna go for '.':
$char = '.';
If you wanna go for 'a':
$char = 'a';
If you wanna go for ';':
$char = ';';
(I added this to help anyone that have any other syntax of string)
To connect you can do(Documentation):
$connection = mysql_connect('adress', 'mysql_user', 'mysql_password');
Okay, we have connection. Now take the data (Documentation).
$database = "DatabaseWithTableWithIntegers";
$table = "TableWithIntegers";
$rowname = "RowWithIntegers";
$sql = "USE $database; SELECT $rowname FROM $table;";
$result = mysql_query ($sql, $connection);
Now declare something like list:
$arr = []; // array for fetching
$vrr = []; // array for fetched
Next step is to fetch rows(Documentation):
while($row = mysql_fetch_row($result)){
Row have string like "2,142,124,53,3511,513,531,53" if separator was an "," and the record exists in database/table.
The magical function is explode, it slices all the segments into an array(Documentation):
$arr = explode($char, $row[$rowname]); //rowname is to be 100% sure
The last thing is to do the conversion(Documentation)
for($arr as &$a){
$value = intval($a);
array_push($vrr, $value); // to save the value into vrr([Documentation][6])
}}
have a look on below solutions:
Method 1
$alphabet = range('a', 'z');
$str = '2,3,5,8,9,21';
$str_array = explode(',', $str);
foreach($str_array as $k=>$v){
${$alphabet[$k]} = $v;
}
echo $a,$b,$c,$d;
Method 2
$var = 'a';
$str = '2,3,5,8,9,21';
$str_array = explode(',', $str);
foreach($str_array as $k=>$v){
${$var} = $v;
++$var;
}
echo $a,$b,$c,$d;
both solution's output:
2358
if you are storing data in form of a1,a2,a3 in a single column, why don't you just fetch the data and split it using explode() method?
if you are using associative array, then use json_encode() when storing the data as string, and then decode it using json_decode() after fetched from database
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 these row in table
K04, K84, K53, K331, L985
how i can do with array explode to get result like :
['K04','K84','K53','K331','L985']
thanks for any help...
i tried to using explode,
$ex = explode (' ,' $myRowFromTable)
$co = count ($ex);
for ($i = 0; $i < $co; $i++)
{
echo $ex[$i];
}
result only show K04
Seems like you want to convert String to JavaScript Array. Take a look for Example;
$myRowFromTable = 'K04, K84, K53, K331, L985';
// Has Array Data
$ex = explode (', ', $myRowFromTable);
// print_r($ex);
// According to User Expected Result
$newFormat = "['" . implode("', '", $ex) . "']";
echo $newFormat;
DEMO
You should try using explode as below assuming you are getting the values as a string in $myRowFromTable:
$ex = explode (',', $myRowFromTable); //no space before comma