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.
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:
Merge two flat indexed arrays of equal size so that values are pushed into the result in an alternating fashion
(2 answers)
Closed last month.
I have two arrays:
$array1 = array("A","One","C","Z");
$array2 = array("B","K","2","5");
Is there some built-in PHP way to get a final array with (I don't know how to say it, maybe 1-1 correspondence addition) alternate keys appended to each other like this
$final_array = array("A","B","One","K","C","2","Z","5");
If I use array_merge, I get:
$final_array = array("A","One","C","Z","B","K","2","5")
But that's exactly what an array_merge does. Is there any workaround other than looping?
Try this:
$array1 = array(1,3,5,7);
$array2 = array(2,4,6,8);
$final_array = array_merge($array1,$array2);
sort($final_array);
print_r($final_array);
Hope this helps. Sorted the array using the sort function.
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
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;