Getting rid of duplicate array - php

Array is like this:
Array
(
[0] => 2011/10/05
)
Array
(
[0] => 2011/10/05
)
How can I get rid of the duplicate array?

php has a function
array_unique
http://php.net/manual/en/function.array-unique.php
EDIT
Option 1
<?php
$array1 = array("2011/10/05");
$array2 = array("2011/10/05");
$merged = $array1;
foreach($array2 as $v) array_push($merged,$v);
$unique = array_unique($merged);
?>
You could use array_merge, but the problem with array_merge is that it joins the same keys together which you probably don't want. The above code will add elements from array2 to array1 and then do array_unique (adding elements of array2 to array1 can also be done differently).

First merge and then unique
$a = array('2011/10/05');
$b = array('2011/10/05');
$c = array_merge($a,$b);
$d = array_unique($c);

Related

How to delete array of element in array

I have two array. I want to remove if 2nd array exists in 1st array. For example
array1 = array ("apple","banana","papaya","watermelon","avocado");
array2 = array ("apple","avocado");
I want the output should be
Array ( [1] => banana [2] => papaya [3] => watermelon)
Here are some code that I'd tried.
foreach($array2 as $key){
$keyToDelete = array_search($key, $array1);
unset($array1[$keyToDelete]);
}
print_r($array1);
but the output is
Array ( [1] => banana [2] => papaya [3] => watermelon [4] =>avocado )
It only remove first element.
i also tried to do something like this
$result = array_diff($array1,$array2);
print_r($result);
but the output is it print all element in array1
Noted: I want the result need to be outside foreach loop
array_diff should be work.
<?php
$array1 = array ("apple","banana","papaya","watermelon","avocado");
$array2 = array ("apple","avocado");
$array_diff = array_diff($array1, $array2);
print_r($array_diff);
?>
DEMO
output will be.
Array ( [1] => banana [2] => papaya [3] => watermelon)
You can also try below solution. result will be same.. using in_array Check if first array value not in the second tester that value in the new array 'final_result' for results.
in_array support (PHP 4, PHP 5, PHP 7)
$array1 = array ("apple","banana","papaya","watermelon","avocado");
$array2 = array ("apple","avocado");
$final_result = array();
foreach($array1 as $value){
if(!in_array($value, $array2)){
$final_result[] = $value;
}
}
print_r($final_result);
?>
DEMO
With the help of array_filter() we can do it easily. It filters elements of an array using a callback function.
array_filter() iterates over each value in the input array passing them to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.
Here we have used use($array2) clause to access the external variable inside callback function. $array2 is needed to filter out $array1.
$array1 = array("apple","banana","papaya","watermelon","avocado");
$array2 = array("apple","avocado");
$array1 = array_filter($array1, function($item) use($array2) { return !in_array($item, $array2); });
print '<pre>';
print_r($array1);
Demo
The fastest way to do this is to create a set(associative array) of elements in $array2 and iterate over $array1 and check if element in $array1 exists in our set or not using isset(). We take advantage of the method/algorithm technique called hashing.
<?php
$array1 = array ("apple","banana","papaya","watermelon","avocado");
$array2 = array ("apple","avocado");
$set = [];
foreach($array2 as $element){
$set[$element] = true;
}
$result = [];
foreach($array1 as $element){
if(!isset($set[$element])){
$result[] = $element;
}
}
print_r($result);
Demo: https://3v4l.org/PcS45

How to push a found array item into another array?

I have an array. But trying to separate items. For example:
$array1 = ["banana","car","carrot"];
trying to push car into another array which is $array2
$push = array_push($array1, "car") = $array2;
I try to find array_push usage for this but the documentation is all about. sending new item to array. Not array to array. Is this possible with array_push or need to use something else?
I need to search for the value car in $array1, push it into $array2 and remove it from $array1.
Here is a flexible solution that allows you to search by "car", then IF it exists, it will be pushed into the second array and omitted from the first.
Code: (Demo)
$array1 = ["banana","car","carrot"];
$needle = "car";
if (($index = array_search($needle, $array1)) !== false) {
$array2[] = $array1[$index];
unset($array1[$index]);
}
var_export($array1);
echo "\n---\n";
var_export($array2);
Output:
array (
0 => 'banana',
2 => 'carrot',
)
---
array (
0 => 'car',
)
You can push $array1 item car into $array2 as like below
$array2 = array();
$array1 = ["banana","car","carrot"];
array_push($array2, $array1[1]);
print_r($array2); /* OutPut Array ( [0] => car ) */
As you know $array1[1] has value is car so it will push it into $array2 by suing php built-in function array_push()
Hope this will help you.
$array1 = ["banana","car","carrot"];
$array2 = array_slice($array1, 1, 1);
unset($array1[1]);

How can i split array into two array based on integer value

Here is an example array I want to split:
(1428,217,1428)
How do I split it in 2 array like this?
(1428,1428)
(217)
I have tried following way but it's only return 1428 array.
$counts = array_count_values($array);
$filtered = array_filter($array, function ($value) use ($counts) {
return $counts[$value] > 1;
});
One way to solve this for your example data is to sort the array and use array_shift to get the first element of the array and store that in an array.
$a = [1428,217,1428];
sort($a);
$b = [array_shift($a)];
print_r($a);
print_r($b);
Result
Array
(
[0] => 1428
[1] => 1428
)
Array
(
[0] => 217
)
You can try this.
$array = array(1428,217,1428);
$array1 = array_slice($array, 0, 2);
$array2 = array_slice($array, 2, 3);
print_r($array1);
print_r($array2);
And the output will like this:-
Array
(
[0] => 1428
[1] => 217
)
Array
(
[0] => 1428
)
In your case it will only return 1428 since array_count_values returns an array with values as keys and their frequency as array value therefore $counts will be equal to array('1428' => 2, '217' => 1);
If I understood your question well you should do something like this:
$array1 = [1428, 217, 1428];
$result = [];
foreach($array1 as $value){
$result[$value][] = $value;
}
This will not create an array for each different value but will create a new element for each unique value in $result. The final value of $result will be array('1428' => [1428, 1428], '217' => [217]) . Which can be easily manipulated as if they were 2 different arrays.
Let me know if this works for you, if not I will try to update my answer according to your specification.

Create an array of key difference using two array

How can we find key deference array using two arrays like
First Array :
$array_1 = array('300','200','500');
Second Array :
$array_2 = array('500','300','200');
$array_2 is generating by applying rsort to $array_1
Then i want to generate an array of key by comparing value of $array_1 and key of $array_2.Output will be an array of
$key_array = ('1','2','0');
Use array_flip() on $array_2 to convert the keys to values and vice versa. Then you can easily find the original keys.
$flip_2 = array_flip($array_2);
$key_array = array_map(function($el) use ($flip_2) { return $flip_2[$el]; }, $array_1);
DEMO
Try:
$array_1 = array('300','200','500');
$array_2 = array('500','300','200');
$key_array = array();
foreach($array_1 as $arr1) {
$key_array[] = array_search($arr1, $array_2); // get key in array_2 for value of array1
}
print_r($key_array);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 0
)

How to intersect the more than four arrays with empty array using PHP

I need to intersect more than fourarray with empty arrays .
I have four arrays:
array1 = array('10','20','36');
array2 = array();
array3 = array ('20');
array4 = array ('20','40');
How can I intersect the array ? . I need to get 20 as result ?
Try array_intersect
your array2 was empty, then you wont get any common elements.
$array1 = array('10','20','36');
//$array2 = array();
$array3 = array ('20');
$array4 = array ('20','40');
//$a = array_intersect ( $array1 ,array2, $array3, $array4 );
$a = array_intersect ( $array1 , $array3, $array4 );
print_r($a); //Array( [1] => 20);

Categories