This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 2 years ago.
How can I remove duplicate comma separated values returned from database?
I have it in this format:
XL,L,XXL
XL
M,L,XL
L,XL,M
What I tried:
$new_str = array_unique(array_map('trim', explode(',', $srow['size'])));
$string = implode(',', $new_str);
But I still get duplicate values. Is there a better way of doing it?
Since your data is stored as comma separated values in the database, you will need to aggregate all the values as you loop over fetching from the database and then remove duplicates e.g.
while ($srow = $result->fetch_assoc()) {
$sizes[] = $srow['size'];
// do other stuff with fetched data
// ...
}
$sizes = array_unique(array_map('trim', explode(',', implode(',', $sizes))));
sort($sizes);
$string = implode(',', $sizes);
Demo on 3v4l.org (with simulated database fetch)
Related
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 3 years ago.
im trying to remove from my string the comma separeted and multiples strings and then to count strings as total, i have no luck so.. any help will be apreciated, thanks!
Example names [alex,daniel,obama,alex,alex,alex,diana] = 7 names and without duplicates = 4 as total. This i'm trying to achive.
Here's the Php code:
$q = mysqli_query($db,"SELECT DISTINCT(names) FROM users WHERE uid = 1 ");
while ($data = mysqli_fetch_array($q)) {
$names = count($data['names']);
echo implode(',', array_keys(array_flip(explode(',',$names))));
}
If $data['names'] is a comma separated list of names, you can get only the unique values like this:
$names = array_unique(explode(',', $data['names']));
$count = count($names);
$names = implode(',', $names);
echo "$count names: $names\n";
For your sample data, this gives
4 names: alex,daniel,obama,diana
Demo on 3v4l.org
This question already has answers here:
Transpose multidimensional array and join values with commas [duplicate]
(2 answers)
Closed 10 months ago.
I am trying to achieve the following functionality:
There is 5 textareas, the user inputs several words on different lines into the textarea, on click of a button it creates arrays from these textareas and the merges the array whilst appending the corresponding keys.
Textarea 1:
example 1
example 1 other
Textarea 2:
example 2
example 2 other
Using:
$col1 = $_POST['txta1']; $col1Array = explode("\n", str_replace("\r", "", $col1));
$col2 = $_POST['txta2']; $col2Array = explode("\n", str_replace("\r", "", $col2));
This will now give me an array for each keyword, separating the textarea value based on a new line.
I now want to combine the 2 arrays so that key [0] appends the first array and so on, it should become
array([0]=>'example 1 example 2',[1]=>'example 1 other example 2 other');
In order for me to the echo out into another textarea, the results whilst should be:
example 1 example 2
example 1 other example 2 other
You can pass both the arrays to array_map to pivot them into the format you want, then implode the inner arrays so you end up with an array of strings rather than an array of arrays.
$result = array_map(
function(...$row) { return implode(' ', $row); },
$col1Array, $col2Array
);
This question already has answers here:
Remove empty array elements
(27 answers)
Closed 6 years ago.
I have a textarea box that takes barcodes separated by a new line. When they are processed, the new lines become commas. But if somebody inputs:
123456
234567
...
with a bunch of spaces under each code as such, it then becomes
$barcode_list = 123456,,,234567
I am able to strip the commas from the end, and I have tried:
$array = explode(",", $barcode_list);
foreach($array as $item){ //Separate each item
if ($item == "") {
unset($item);
}
but it doens't seem to work, I still get mysqli errors etc. Is there any way to get around this?
You should first remove unnecessary commas, then explode. Fewer steps...
// every sequence of commas becomes one comma
$barcode_list=preg_replace("/,+/",",",$barcode_list);
// explode the string into an array
$array = explode(",", $barcode_list);
You're doing an unset that doesn't really affect your array.
Do this:
$array = explode(",", $barcode_list);
foreach($array as $key => $item){ //Separate each item
if ($item == "") {
unset($array[$key]);
}
}
This question already has answers here:
php count the number of strings after exploded
(5 answers)
Closed 8 years ago.
I need to create a text field that can get multiple input like this...
1, 2, 3, 4
Then the output should be...
Number of input: 4
Mean: 2.5
The problem is how can I count the number of the input, I mean how the program know that how many input that user type into the text field, and use each value to calculate a summation for all. Does anybody has any methods or any idea?
Thanks.
Use explode(); and explode all the commas (,). That will give you an array.
From there, use count and a loop for counting and get the mean. Use trim() aswel for getting rid of empty spaces.
You can test the code here: http://writecodeonline.com/php/
$input = "1, 2, 3, 4";
//remove all whitespace
$input = str_replace(' ', '', $input);
//turn the string into an array of integers
$numbers= array_map('intval', explode(',', $input));
//the number of elements
$count = count( $numbers );
//sum the numbers
$sum = array_sum( $numbers );
//print the number of inputs, a new line character, and the mean
echo "Number of inputs: $count\r\n";
echo 'Mean: ' . $sum / $count;
I have a script that will insert a value in a cell, in my database. That cell will contain 4 values total. It should look like this: 0;0;0;0 (Each '0', represents a value)
How can I insert example value 100, at the place where '0' number 3 is, so it will look like this:
0;0;100;0
Thanks in advance.
This is bad database design and breaks the first normal form of database design.
I would recommend re-thinking your schema and data architecture.
Maybe break them out into individual columns.
Your data should be designed relationally to minimize repeating patterns in your columns (see link above)
I can almost guarantee you that there is a better way...
look into serialize() and unserialize()
$array = array(0,20,103,330);
$string = serialize($array); // store this in db
then get the string from db:
$array = unserialize($string);
access/update values with the array and re-store in db
Or if you are stuck with the format:
$string = '0;0;100;0'; // coming from db
$array = explode(';' , $string);
$array[2] = 100;
$string = implode(';' , $array);
<?php
// original string of values seperated by colon
$string = "0;0;0;0";
// create array of values by splitting at colons
$vals = preg_split('/;/', $string);
// modify the value of any elements in your array
$vals[2] = 100;
// glue your array of values together with semicolons at the joins
$string = implode(';',$vals);
// check the value has been changed coreectly
echo $string;
?>