This question already has answers here:
php remove duplicates from array
(5 answers)
Closed 9 years ago.
How do I remove duplicates from an array?
Let's say that my I have two arrays named $array and $new_array. $array has contents while $new_array is empty, as seen below:
$array = array(5,1,2,1,5,7,10);
$new_array = array();
I want $new_array to store the unique values of $array. It kind of goes like this:
$array = array(5,1,2,1,5,7,10);
$new_array = array(5,1,2,7,10); // removing the 1 and 5 after 2 since those numbers are already a duplicate of the preceding numbers.
echo $new_array; // Output: 512710
You can do it through PHP's array_unique function.
This function traverses through your provided array and returns an array with unique values (repeating values will be removed).
Code to return desired string:
$array = array(5,1,2,1,5,7,10);
$new_array = array_unique($array);
echo implode('', $new_array);
Use array_unique() and implode():
$array = array(5,1,2,1,5,7,10);
$new_array = array_unique($array);
echo implode('', $new_array);
Output:
512710
Related
This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 5 years ago.
"2017-08-31":["5948a0dd21146a43fdcfef5a","5948a0dd21146a43fdcfef5a"]
"2017-08-22":["5948a0dd21146a43fdcfef5a"]
"2017-08-09":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]
"2017-08-08":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]
I have an array like this, key is a date and multiple value associated with that date, but I need unique value with that key.how to do that ?
I've tried with array_unique but no luck!
Just use array_map and array_unique together.
<?php
$a = json_decode('{"2017-08-31":["5948a0dd21146a43fdcfef5a","5948a0dd21146a43fdcfef5a"],
"2017-08-22":["5948a0dd21146a43fdcfef5a"],
"2017-08-09":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"],
"2017-08-08":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]}', true);
echo json_encode(array_map("array_unique", $a));
$array = your array
$array = array_map(function($val){return array_unique($val);}, $array);
You better do this using SQL if it's possible. Otherwise, you can try this:
$array = array_unique($array, SORT_REGULAR);
This question already has answers here:
How to use array values as keys without loops? [duplicate]
(6 answers)
Closed 6 years ago.
Please check attached images. These image contains $complex_arr print and $simple_arr print results. I want to convert $complex_arr to $simple_arr output.
How is that possible? What is actually doing each of $complex_arr inside value will be converted as associative simple array like $simple_arr
$arr1 = array("asso1"=>"a", "asso2"=>"1");
$arr2 = array("asso1"=>"b", "asso2"=>"2");
$arr3 = array("asso1"=>"c", "asso2"=>"3");
$complex_arr = array($arr1,$arr2,$arr3);
$simple_arr = array("a"=>"1", "b"=>"2", "c"=>"3");
// print_r($complex_arr);
print_r($simple_arr);
Input:
Output:
You have to write it on our own... here is my idea:
public function makeItSimpler($arr){
$newarr = array();
foreach($arr as $complex){
$newarr[$complex['asso1']]=$complex['asso2'];
}
return $newarr;
}
Perhaps you can do it better... take look at "array-map" http://php.net/manual/de/function.array-map.php
Good Luck
foreach ($complex_arr as $key => $value) {
$simple_arr[$value['asso1']]=$simple_arr[$value['asso2']];
}
With php5.5 (where array_column becomes available) it is:
$simple_arr = array_combine(
array_column($complex_arr, 'asso1'),
array_column($complex_arr, 'asso2')
);
And even simplier (after I reread function manual):
$simple_arr = array_column($complex_arr, 'asso2', 'asso1');
This question already has answers here:
Eliminating duplicate values using PHP
(3 answers)
Closed 9 years ago.
I want to get the common value as well as different value from same array
For example :
$array1 = array(1,2,2,2,3,3,4,4,4,5,5,5,5,6,6,6,7,8,9,10);
And I want the array as
$array1 = array(1,2,3,4,5,6,7,8,9,10);
Can anyone give me the idea to bring the array like this
Why don't you try
$array1 = array(1,2,2,2,3,3,4,4,4,5,5,5,5,6,6,6,7,8,9,10);
$array1 = array_unique($array1);
http://php.net/manual/en/function.array-unique.php
Use array unique
array_unique($array1);
$array1 = array(1,2,2,2,3,3,4,4,4,5,5,5,5,6,6,6,7,8,9,10);
$array = array_unique($array1);
print_r($array )
see array_unique . It will remove the Duplicate values from array
try this :
$array1 = array(1,2,2,2,3,3,4,4,4,5,5,5,5,6,6,6,7,8,9,10);
$unique_array=array_unique($array1);
print_r($unique_array);
use array_unique(). It reduceds an array.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to remove values from an array in PHP?
I have this
$arr1 = array('orange','banana');
$arr2 = array('broccli','tomato','mixedvegies','orange','veg2');
how would I take complete $arr1 from $arr2 so that arr2 has
$arr2 = array('broccli','tomato','mixedvegies','veg2');
array_diff($arr2, $arr1);
array_diff returns an array with all the elements not in the second (or third, fourth... optional args).
You can use array_diff for this:
$uniqueItems = array_diff($arr2, $arr1);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create comma separated list from array in PHP?
I have an array as follows;
$array = array(1,2,3,4,5);
I want to print or echo this variable as 1,2,3,4,5. What is the simplest method for this?? I printed array[0] first and then skipped first value and used foreach function to echo all remaining ",".$value.
Try following
echo implode(",", $array);
You can use the implode function.
In the example you showed, it'd be written like this:
implode(',', $array);
$array = array(1,2,3,4,5);
$result = implode(',', $array);
echo $result;