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
Related
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)
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:
Split a comma-delimited string into an array?
(8 answers)
Closed 5 years ago.
for example i have this input in my form 12345,6789,1011
and i want to get their first number the output should be like this 1 6 1 just want to get all the first number of separated comma in my code all I'm getting is the first part of the number 12345 = 1 couldn't get the other number that is separated by comma,
thanks in advance for the help.
You can do like below:
<?php
$string = "12345,6789,1011";
$result = explode(",",$string);
$ConcatString = '';
foreach($result as $Values)
{
$ConcatString .= $Values[0]." ";
}
echo $ConcatString;
?>
Output will be 1 6 1
This question already has answers here:
How can you make a multidimensional array unique? [duplicate]
(6 answers)
Closed 5 years ago.
I have an Array which has 3 Dimensions:
$data_ary[$k_0][$v_0[4]]['...'] = ...
In the brackets with '...' I write Parameters like "Country", "Designation", "Year" and so on. Now as I run this through a loop the values for $k_0 increases every loop by 1 and $v_0[4] always changes the specific value.
My Problem: Very often the values are duplicates across the different dimensions. For Example:
$data_ary[1][1]['Country'] = 'Germany';
$data_ary[1][1]['Year'] = '2017';
$data_ary[2][1]['Country'] = 'Germany';
$data_ary[2][1]['Year'] = '2017';
How do I delete those duplicates? I have tried array_unique() but the result was that from the 27.000 entries 26999 got deleted..
Some Example Input:
$data_ary[6]['GERMANY']['YEAR'] = 2017;
$data_ary[6]['GERMANY']['MONTH'] = 1;
$data_ary[6]['GERMANY']['ID'] = 6010;
$data_ary[6]['GERMANY']['COUNTRY'] = 'GERMANY';
$data_ary[7]['ITALY']['YEAR'] = 2016;
$data_ary[7]['ITALY']['MONTH'] = 4;
$data_ary[7]['ITALY']['ID'] = 52752;
$data_ary[7]['ITALY']['COUNTRY'] = 'ITALY';
$data_ary[8]['GERMANY']['YEAR'] = 2017;
$data_ary[8]['GERMANY']['MONTH'] = 1;
$data_ary[8]['GERMANY']['ID'] = 6010;
$data_ary[8]['GERMANY']['COUNTRY'] = 'GERMANY';
As you can See the first and the second are basically the same but differ by the value in the first bracktes.
You can do it like below:-
$input = array_map("unserialize", array_unique(array_map("serialize", $data_ary)));
print_r($input);
Output:- https://eval.in/903355
Reference:- How to remove duplicate values from a multi-dimensional array in PHP
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;