unique array values - php

I have string of comma separated values.
1,2,3,4,4,4,4,4,4,4,4,01633,4,4
I need to remove the duplicates, so I though of using
array_unique($str);
However, it returns no results. So I decided to output it to see what I have:
print_r($str);
// returns: 1,2,3,4,4,4,4,4,4,4,4,01633,4,4
I'm a little lost. I checked if it is an array and I got true. Here's how that string is created:
$str = '1,2,3';
foreach ($a as $b) {
$str.= ','.$b;
}
What am I missing here?

$str = explode(',', $str); // create array
$newArray = array_unique($str); // then process
actually, though... just do your array_unique() on $a before the string is created.
$a = array_unique($a);
then...
foreach ($a as $b) { // and so on

Convert to an array, remove the repeat values, convert to string
$str = 'whatever';
$arr = explode( ',', $str );
$newArr = array_unique( $arr );
$newStr = implode( ',', $newArr );

Explode on comma, make unique, glue pieces back together:
$str = implode(',', array_unique(explode(',', $str)));

Related

Merge two strings

I have a string stored in my table. It's a number of values separated by pipe:
val 1|val2|val 44
I also have another string, separated by semi-colons that I would like to merge unique values from into the first sting.
abcd; efg; hijklmnop3; val2
So I thought that the easiest way would be to convert them into arrays, merge and keep unique and the implode back into the string.
(my loop) {
$arr1 = array();
$arr2 = array();
$arr1[] = explode(';', $str1);
$arr2[] = explode('|', $str2);
$arr3 = implode("|",array_unique(array_merge($arr1,$arr2)));
}
But when I try to
echo $arr3;
I get
Warning: Array to string conversion
What am I missing here?
Just simplify to following:
$str1 = 'abcd; efg; hijklmnop3; val2';
$str2 = 'val 1|val2|val 44 ';
// explode results an array
$arr1 = array_map('trim', explode(';', $str1));
$arr2 = array_map('trim', explode('|', $str2));
// Implode results a string
$string = implode("|", array_unique(array_merge($arr1, $arr2)));
echo $string;
Results in
abcd|efg|hijklmnop3|val2|val 1|val 44
Note, that explode already returns an array. With the following you add an array to an array, but you just want the array.
$arr1[] = explode(';', $str1); is the same as array_push($arr1, explode(';', $str1));
The unique of your question failed, because you need to trim the spaces.
'val2' !== ' val2' <-- see the space

how to add commas before,in between and after the array in php

How should i add the commas before,in between and after the array in php
here is my code
$arr = array(1,2,3,4,5,6,7,8,9);
$string = implode(',', $arr);
echo $string;
getting output: 1,2,3,4,5,6,7,8,9
expected output : ,1,2,3,4,5,6,7,8,9,
Just concatenate those commas
$string = ','.implode(',', $arr).',';
Another way
$arr = array(1,2,3,4,5,6,7,8,9);
$str = '';
foreach($arr as $ar){
$str .= "-{$ar}";
};
echo $str.'-';

preg_replace php to replace repeated characters

So I have a list of values like that goes like this:
values: n,b,f,d,e,b,f,ff`
I want to use preg_replace() in order to remove the repeated characters from the list of values (it will be inserted to a MySQL table). b and f are repeated. ff should not count as f because it's a different value. I know that \b \b will be used for that. I am not sure on how to take out the repeated b and f values as well as the , that precedes each value.
If the list is in a string looking like the example above, a regex is overkill. This does it just as well;
$value = implode(',', array_unique(explode(',', $value)));
I agree with other commenters that preg_replace is not the way to go; but, since you ask, you can write:
$str = preg_replace('/\b(\w+),(?=.*\b\1\b)/', '', $str);
That will remove all but the last instance of a given list-element.
No need for regex for this:
join(",", array_unique(split(",", $values)))
If this list you're dealing with is a simple string, a possible solution would be like this:
function removeDuplicates($str) {
$arr = explode(',', $str);
$arr = array_unique($arr);
return implode(',', $arr);
}
$values = removeDuplicates('n,b,f,d,e,b,f,ff'); // n,b,f,d,e,ff
$str = "values: n,b,f,d,e,b,f,ff";
$arr = array();
preg_match("/(values: )([a-z,]+)/i", $str, $match);
$values = explode(",", $match[2]);
foreach($values AS $value){
if(!$arr[$value]) $arr[$value] = true;
}
$return = $match[1];
foreach($arr AS $a){
$return .= ($i++ >= 1 ? "," : "").$a;
}

PHP - split String in Key/Value pairs

I have a string like this:
key=value, key2=value2
and I would like to parse it into something like this:
array(
"key" => "value",
"key2" => "value2"
)
I could do something like
$parts = explode(",", $string)
$parts = array_map("trim", $parts);
foreach($parts as $currentPart)
{
list($key, $value) = explode("=", $currentPart);
$keyValues[$key] = $value;
}
But this seems ridiciulous. There must be some way to do this smarter with PHP right?
If you don't mind using regex ...
$str = "key=value, key2=value2";
preg_match_all("/([^,= ]+)=([^,= ]+)/", $str, $r);
$result = array_combine($r[1], $r[2]);
var_dump($result);
<?php parse_str(str_replace(", ", "&", "key=value, key2=value2"), $array); ?>
if you change your string to use & instead of , as the delimiter, you can use parse_str()
If you can change the format of the string to conform to a URL query string (using & instead of ,, among other things, you can use parse_str. Be sure to use the two parameter option.
Here's a single command solution using array_reduce formatted in multple lines for readability:
<?php
$str = "key=value, key2=value2";
$result = array_reduce(
explode(',', $str),
function ($carry, $kvp) {
list($key, $value)=explode('=', $kvp);
$carry[trim($key)]=trim($value);
return $carry;
}, []);

PHP: Split string into 2D array

I have a string which contains quite a bit of data. I want to split the data into a 2D array. The data in the string is split by a ~ (tilde) for the columns and a : (colon) for the different rows.
An example string could be: "London~10~20~cold:New York~23~53~hot:Madrid~43~12~dry".
Thanks.
$string = "London~10~20~cold:New York~23~53~hot:Madrid~43~12~dry";
$array = explode(':', $string);
foreach($array as &$value) $value = explode('~', $value);
Functional way (PHP 5.3.x needed):
$string = "London~10~20~cold:New York~23~53~hot:Madrid~43~12~dry";
$map = array_map(function($el) {
return explode('~', $el);
}, explode(':', $string));
Another alternative would be:
preg_match_all('/(.*?)~(.*?)~(.*?)~(.*?)(?:$|:)/', $string, $array,
PREG_SET_ORDER);
It's more cumbersome in that you have to predefine the column format. It also returns the complete match in each rows [0]. Otherwise (due to PREG_SET_ORDER) it's in your desired 2d format.
Just posting it here to please and annoy the microoptimizers at the same time. Despite the common Stackoverflow meme, the regex is three times faster than the explode loop.
You can split data in php with explode().
So first you have to split the string, than you have to split your entries again with explode().
$data = explode(':', $string);
$array = array();
foreach($data as $d) {
$d = explode('~', $d);
$array[] = $d; //e.g. $array[0][0] --> London
//$array[$d[0]] = array('temperature' => $d[1], 'dont-know' => $d[2], 'climate' => $d[3]); //e.g. $arra['London'] => array(...)
}
A "functional" style
$array = array_map(function($value) {
return explode('~', $value);
}, explode(':',$string););

Categories