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
Related
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 last year.
Improve this question
I got this string
$string = '1:23:health,2:24:mana';
$v = explode(",", $string);
foreach($v as $data)
{
echo $data[0][1];
}
What im looking is to making a "multidimensional string" by exploding ','
Wanna do this:
data[0][0] = 1;
data[0][1] = 23;
data[0][2] = "health";
How can i do this in PHP?
$string = '1:23:health,2:24:mana';
$result = [];
foreach (explode(',', $string) as $step) {
$result[] = explode(':', $step);
}
print_r($result);
An alternative is:
$string = '1:23:health,2:24:mana';
$result = array_chunk(preg_split('/[:,]/', $string), 3);
print_r($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 3 years ago.
Improve this question
For example: if strArr is ["B:-1", "A:1", "B:3", "A:5"] then your program should return the string A:6,B:2.
Your final output string should return the keys in alphabetical order. Exclude keys that have a value of 0 after being summed up.
Examples
Input: array("X:-1", "Y:1", "X:-4", "B:3", "X:5")
Output: B:3,Y:1
Simple homework. You have to explode each pair into an array and to sum the values for each letter. Then sort (ksort by key), check and skip if there is a 0 into the produced array:
<?php
$sums = array();
$arr = array("X:-1", "Y:1", "X:-4", "B:3", "X:5");
foreach ($arr as $key => $pair) {
$pairArray = explode(":", $pair);
(!array_key_exists($pairArray[0], $sums))
? $sums[$pairArray[0]] = (int)$pairArray[1]
: $sums[$pairArray[0]] += (int)$pairArray[1];
}
print_r($sums);
ksort($sums);
$result = array();
foreach ($sums as $key => $value) {
if ($value != 0) {
array_push($result, $key . ":" . $value);
}
}
$result = implode(",", $result);
echo $result;
?>
Output:
Array
(
[X] => 0
[Y] => 1
[B] => 3
)
B:3,Y:1
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 3 years ago.
Improve this question
(PHP) how can I split a string in multiple strings separated by comma,
like is I have a string "Hello my neighbor",
then after the split process, it should be
("Hello my neighbor","Hello my","Hello neighbor","my neighbor","Hello","my","neighbor")
try this :
<?php
$string = "Hello my neighbor";
$str = explode(" ",$string);
$newArray = [];
$newArray[] = $string;
for($i = 0; $i<count($str); $i++) {
if($i <= 1) {
$newArray[] = $str[$i]." ".$str[$i+1];
if($i != 0) {
$newArray[] = $str[$i-1]." ".$str[$i+1];
}
}
$newArray[] = $str[$i];
}
echo "<pre>";
print_r($newArray);
echo "</pre>";
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 6 years ago.
Improve this question
i can separate join column with comma, but how to remove comma if on the second column is null?
in below, i have 2 column i want join otot2 and otot3
1. how to not shown comma after otot2 if otot3 null?
2. and how to not shown anything comma if otot2 and otot 3 null?
3. otot2 and otot3 flexible ( not always has data)
here my php code for output query:
if(mysqli_num_rows($hasil) > 0)
{
$response = array();
while($data = mysqli_fetch_array($hasil))
{
$h['main muscle'] = $data['otot1'];
$h['other muscle'] = $data['otot2'].', '.$data['otot3'];
array_push($response, $h);
}
$response["success"];
echo json_encode($response);
A simple solution would be -
$response = array();
while($data = mysqli_fetch_array($hasil))
{
// other code
$temp = array($data['otot2'], $data['otot3']); // Store them in array
$temp = array_filter($temp); // remove empty values
$h['other muscle'] = implode(',', $temp); // implode them with (,)
// Other codes
array_push($response, $h);
}
By using array_filter & implode you don't have to worry about the ,, as array_filter would remove the empty values and if there is 2 values in the array them the string would be , separated else not.
Or you can try this also
$h['other muscle'] = $data['otot2'];
if($data['otot3'] !== null)
$h['other muscle'] .= ', ' . $data['otot3']; // Concatenate
Demo
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