Remove duplicates in array - php

Here is the code for storing unique values to a variable.
$filtermodel = array_unique($list2);
Output:
Array
(
[0] => 15592
[1] => 182
[4] => 14208
[7] => 183
[11] => 184,185,186
[15] => 174
[16] => 177
[23] => 184,186
[24] => 184,185,186,187
[29] => 179
[38] => 188
[41] => 174,176,184,185,186,187
)
Above array key[11] contains 184,185,186 and key[23][24] contains same as key[11]. How to remove those duplicates in array? Any help? Thanks in advance.

You can go with a foreach over each element in array and explode by comma , then do a unique on the resulted list of strings
Here is a Demo

If we suppose that the '182,186' for example is an array of integers and got right what you are trying to achieve the below works :
If not then please consider pointing out better what are you trying to achieve
$mArray = array(
15592,
182,
14208,
183,
array(184,185,186),
174,
177,
array(184,186),
array(184,185,186,187),
179,
188,
array(174,176,184,185,186,187),
);
$mFinalArray = [];
foreach($mArray as $currentObject)
if(!is_array($currentObject) && !in_array($currentObject, $mFinalArray))
$mFinalArray[] = $currentObject;
else if(is_array($currentObject))
foreach($currentObject as $currentObjectInside)
if(!in_array($currentObjectInside, $mFinalArray))
$mFinalArray[] = $currentObjectInside;
sort($mFinalArray);
print_r($mFinalArray);
Output :
Array
(
[0] => 174
[1] => 176
[2] => 177
[3] => 179
[4] => 182
[5] => 183
[6] => 184
[7] => 185
[8] => 186
[9] => 187
[10] => 188
[11] => 14208
[12] => 15592
)

You can use array_walk with passing by reference, using the third parameter (userdata) as dictionary for used values:
array_walk($array, function(&$item, $_, &$dict) {
$values = explode(',', $item);
$item = implode(',', array_filter($values, function ($value) use (&$dict) {
// If value already in the dictionary, just skip it.
if (isset($dict[$value])) {
return false;
}
// Add value to the dictionary and not filter it out.
return $dict[$value] = true;
}));
}, []);
Here is working demo.

Try using a foreach loop and for each row contains ',' symbol try using explode.
You can also create an array just for holding the existing values for later check if they exist.
Simple example-
$unique_values = [];
$all_values = array (
[0] => 15592
[1] => 182
[4] => 14208
[7] => 183
[11] => 184,185,186
[15] => 174
[16] => 177
[23] => 184,186
[24] => 184,185,186,187
[29] => 179
[38] => 188
[41] => 174,176,184,185,186,187
);
foreach( $all_values as $av ) {
if( strpos( $av, ',' ) !== false ) {
$av = explode( ',', $av );
foreach( $av as $a ) {
array_push( $unique_values, $a );
array_unique( $unique_values );
$result = !empty( array_intersect( $unique_values, $a ) );
}
}
}
I didn't test this code but i made it for explaining the logic.

Related

Merge Subarray (PHP 5.3.3) [duplicate]

This question already has answers here:
Merge all sub arrays into one [duplicate]
(3 answers)
Closed 2 years ago.
I want to merge all sub arrays of an array in an older project that requires >=5.3.3.
The array mentioned above looks like this:
$array = Array (
Array
(
[0] => 26
[1] => 644
)
Array
(
[0] => 20
[1] => 26
[2] => 644
)
Array
(
[0] => 26
)
Array
(
[0] => 47
)
Array
(
[0] => 47
[1] => 3
[2] => 18
)
Array
(
[0] => 26
[1] => 18
)
Array
(
[0] => 26
[1] => 644
[2] => 24
[3] => 198
[4] => 8
[5] => 6
[6] => 41
[7] => 31
)
Array
(
[0] => 26
[1] => 644
[2] => 24
[3] => 198
[4] => 12
[5] => 25
[6] => 41
[7] => 31
)
Array
(
[0] => 198
)
Array
(
[0] => 198
)
Array
(
[0] => 899
))
Now, I'm wondering how I could merge all of those Subrrays into one that looks like this:
Array
(
[1] => 26
[2] => 644
[3] => 20
[4] => 26
[5] => 644
[6] => 26
[7] => 47
[8] => 47
[9] => 3
[10] => 18
[11] => 26
[12] => 18
[13] => 26
[14] => 644
[15] => 24
[16] => 198
[17] => 8
[18] => 6
[19] => 41
[20] => 31
[21] => 26
[22] => 644
[23] => 24
[24] => 198
[25] => 12
[26] => 25
[27] => 41
[28] => 31
[29] => 198
[30] => 198
[31] => 899
)
I know how this could work on a more up to date PHP version. So far on this older version I've tried the following:
print_r(array_merge($array, $emptyArray));
But I get the exact same Array returned.
I also tried something like this:
$result_arr = array();
foreach ($array as $sub_arr) $result_arr = array_merge($result_arr, $sub_arr);
$result_arr = array_unique($result_arr);
print_r($result_arr);
Which tells me my second argument is not an array?
I'm a bit confused and hope someone can shed some light on this issue.
this function, I think, will do it well.
function array_flatten(array $array)
{
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
} else {
$result = array_merge($result, array($key => $value));
}
}
return $result;
}
All you have to do is send your array as a parameter of the function, and display the result.
$arrayToFlatt = Array(
Array(26, 644),
Array(20, 26, 644),
Array(26),
Array(47),
Array(47, 3, 18),
Array(26, 18),
Array(26, 644, 24, 198, 8, 6, 41, 31),
Array(26, 644, 24, 198, 12, 25, 41, 31),
Array(198),
Array(198),
Array(899)
);
echo '<pre>';
print_r(array_flatten($arrayToFlatt));
echo '</pre>';
Working since PHP 4
You could use array_walk_recursive(), which will visit each leaf node of the input array, then add this value to an output array...
$output = array();
array_walk_recursive($array, function ($data) use (&$output) {
$output[] = $data;
});
print_r($output);
Your foreach approach is fine, and works for me. But I don't see why you are filtering with array_unique.
<?php
$data =
[
[1,3,5,7],
[2,4,6,8],
[1,2,4,5]
];
$output = [];
foreach ($data as $sub) {
$output = array_merge($output, $sub);
}
var_export($output);
Output:
array (
0 => 1,
1 => 3,
2 => 5,
3 => 7,
4 => 2,
5 => 4,
6 => 6,
7 => 8,
8 => 1,
9 => 2,
10 => 4,
11 => 5,
)
To flatten with the splat (Php 5.6):
$output = array_merge(...$data);
For pre-splat versions:
$output = call_user_func_array('array_merge', $data);
Or array_reduce:
$output = array_reduce($data, 'array_merge', []);
If in doubt foreach:
$output = [];
foreach($data as $v)
foreach($v as $n)
$output[] = $n;
All the above result in the same zero indexed array.

How to merge mutiple array in a single array

As i searched alot but didn't get perfect solution of merging array in one single array.
These arrays are Dynamic (may be increase in future- would 50+). So for that we have to use count() or for loops to fetch and then merge.
Here's my code which i'm trying to resolve over core level. Please anyone tell me how may receive all values in Single array.
Array(
[0] => Array
(
[0] => 123
[1] => 108
[2] => 58
[3] => 23
)
[1] => Array
(
[0] => 93
[1] => 94
[2] => 95
[3] => 172
[4] => 30
)
[2] => Array
(
[0] => 109
[1] => 81
[2] => 79
[3] => 155 )
)`
My expectation of result is: (which i'm unable to get)
Array
(
[0] => 123
[1] => 108
[2] => 58
[3] => 23
[4] => 93
[5] => 94
[6] => 95
[7] => 172
[8] => 30
[9] => 109
[10] => 81
[11] => 79
[12] => 155
)
Use array_merge with splat operator,
$result = array_merge(...$arr);
array_merge — Merge one or more arrays
Splat operator - Since PHP 5.6, you can use splat operator (...) to create simpler variadic functions (functions that take an undefined number of arguments).
Demo
Output:-
Array
(
[0] => 123
[1] => 108
[2] => 58
[3] => 23
[4] => 93
[5] => 94
[6] => 95
[7] => 172
[8] => 30
[9] => 109
[10] => 81
[11] => 79
[12] => 155
)
using array_merge()
$a[0] = [1,2,3];
$a[1] = [4,5,6];
$a[2] = [7,8,9];
$newArr = [];
$newArr = array_merge($a[0], $a[1], $a[2]);
print_r($newArr);
assuming that your array will grow, you can use foreach like this :
$a[0] = [1,2,3];
$a[1] = [4,5,6];
$a[2] = [7,8,9];
$newArr = [];
foreach($a as $index){
foreach($index as $item){
array_push($newArr, $item);
}
}
As i used this technique. I got my answer in just a simple way. of array_flatten
print_r(array_flatten($overall_arr));
function array_flatten($array) {
$return = array();
foreach ($array as $key => $value) {
if (is_array($value)){
$return = array_merge($return, array_flatten($value));
} else {
$return[$key] = $value;
}
}
return $return;
}

Filter all other values except a specific URL from array of URLs PHP

I want filter array in PHP.
suppose, i have many URLs in a single array.
eg; $someArray ie;
Array (
[0] => javascript:signin('https://login.alibaba.com')
[2] =>http://www.alibaba.com
[3] => http://www.alibaba.com/Products?cd=buyhome
[4] => http://www.alibaba.com/today_new/catalogs/0.html
[5] => http://www.alibaba.com/help/search-for-products.html
[6] => http://resources.alibaba.com/trade_safe/home.htm
[7] => http://us.my.alibaba.com/product/buyoffer/myalibaba/post_buying_lead_no_member.htm [8] => http://www.alibaba.com/sell/sell.htm
[9] => http://sourcing.alibaba.com/rfq_search_list.htm?availability=y&tracelog=sell_br_20111229
[10] => http://www.alibaba.com/help/how_to_sell/join_alibaba.html
[11] => http://us.my.alibaba.com/product/post_product.htm
[12] => http://resources.alibaba.com/
[13] => http://ask.alibaba.com
[14] => http://resources.alibaba.com/forum/trade_related.htm
[15] => http://tradeshow.alibaba.com/
[16] => http://us.my.alibaba.com/
[17] => http://us.my.alibaba.com/mcadmin/inbox/inboxList.htm
[18] => http://us.my.alibaba.com/product/post_product_interface.htm
[20] => http://trademanager.alibaba.com/
[21] => http://www.alibaba.com/trade/servlet/page/static/paid_memberships/index
[22] => http://us.favorite.alibaba.com
[23] => http://www.alibaba.com/trade/help/helpcenter
[24] => http://www.alibaba.com/help
[25] => http://resources.alibaba.com/trade_safe/complaint.html
[26] => http://legal.alibaba.com/legal/site/login/login.htm?site_type=international&language_id=english
[27] => http://www.alibaba.com/help/contact-us.html#askquestion
[28] => javascript:showFeedBackWindow()
[29] => javascript:void(0)
[30] => http://www.example.com/help
[30] => http://www.example.com/about
);
See the bold values.I only want these values in array... means I want to delete all other values except those values have example.com in value.
please help.
I hope this will helps
$someArray = array('http://www.alibaba.com', ' http://us.my.alibaba.com/', 'http://www.example.com/help', 'http://www.example.com/di' );
$goodLink = 'example.com';
foreach ($someArray as $key => $link) {
if(strpos($link, $goodLink) === false) unset($someArray[$key]);
}
Output
Array ( [2] => http://www.example.com/help [3] => http://www.example.com/di )
Use array_filter with a callback function as below
$someArray = array("javascript:signin('https://login.alibaba.com')", "http://www.example.com/help", "http://www.example.com/about", "http://www.alibaba.com");
$new_arr = array_filter($someArray, "filter");
function filter($element) {
if (strpos($element,'example.com') !== false) {
return $element;
}
else {
return;
}
}
print_r($new_arr);
Output
Array
(
[1] => http://www.example.com/help
[2] => http://www.example.com/about
)
Working demo: http://codepad.org/UPVhHvlJ
Use array_map() & strpos(). Example:
$url_array = [
'http://www.google.com/',
'http://www.example.com/help',
'http://www.example.com/about',
'http://www.facebook.com/',
];
$result = array_filter(array_map(function($v){
if(strpos($v, 'example.com') !== false){
return $v;
}
}, $url_array));
print '<pre>';
print_r($result);
print '</pre>';
Reference:
array_map()
array_filter()
strpos()

php array filtering values defined by key from multiple arrays

Hello I'm trying to solve the following issue. I have some code which is self explanatory but I need to add a couple of lines to it
I would like filter the lower value arrays defined by the key value (in this case [2]) via the key value [1]. So if I have 3 arrays which contain key [1] with a value 100, then the arrays should be filtered via key [2].
Example of my code so far:
foreach($data as $line) {
if(substr($line,0,1)=="A") {
if(!$first) {
$parts = explode(chr(9), $line);
list($num1, $num2) = explode('_', $parts[1]); //code comes first / tested and works
$parts[2] = isset($num2) ? $num2 : $parts[2]; //it replaces key[2] with _* (1,2,3)
//then this will follow
$pos = strpos($parts[1], '_'); // this will remove all _* from key [1] if they exist
if($pos !== false) $parts[1] = substr($parts[1], 0, $pos); // tested and works
//echo "<pre>"; print_r($parts); echo "</pre>";
//need code to filter the arrays defined by key [1] via key [2] here?
So for example if I have multiple arrays after my piece of code like this:
Array
(
[0] => A
[1] => 100
[2] => 1
[3] => 1184
[4] => 0
)
Array
(
[0] => A
[1] => 100
[2] => 2
[3] => 1185
[4] => 0
)
Array
(
[0] => A
[1] => 100
[2] => 3
[3] => 1186
[4] => 0
)
Array
(
[0] => A
[1] => 101
[2] => 1
[3] => 1187
[4] => 0
)
Array
(
[0] => A
[1] => 101
[2] => 2
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 302
[2] => 0
[3] => 1161
[4] => 0
)
After some code to filter the arrays, the final example result will be:
Array
(
[0] => A
[1] => 100
[2] => 3
[3] => 1186
[4] => 0
)
Array
(
[0] => A
[1] => 101
[2] => 2
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 302
[2] => 0
[3] => 1161
[4] => 0
)
Please I could do with some help on this it only needs a couple of lines, I'm not a programmer but I'd like to finish this project.
Try this :
$array = array("A", "100_1", 0, 1184, 0);
$array = array_map(
function($str) {
return preg_replace_callback('/([\d]+)\_([1-3])/', function($matches){ return $matches[1] + $matches[2]-1;}, $str);
},
$array
);
print_r($array)
Try this:
$part = array();
foreach ($parts as $key => $value) {
if (isset($part[$value[1]])) {
if ($parts[$part[$value[1]]][0][2] < $value[0][2]) {
$part[$value[1]] = $key;
}
} else {
$part[$value[1]] = $key;
//echo "<pre>"; print_r($part); echo "</pre>";
}
}
I change the answer because it was not what I'm looking for but this is.

Create new array from two array;

I have this two arrays
$views[] = $id;
$pid[] = $page_id;
which prints
Array
(
[0] => 9
[1] => 12
[2] => 13
[3] => 14
[4] => 15
)
Array
(
[0] => 174
[1] => 221
[2] => 174
[3] => 174
[4] => 174
)
now i want to create new array from this result like(first will be the key and second be the value)
Array
(
[9] => 174
[12] => 221
[13] => 174
[14] => 174
[15] => 174
)
I have tired array_push function but didnt work for me.
You can use array_combine:
Creates an array by using one array for keys and another for its
values
ie:
$newarr = array_combine($array1, $array2); //$array1: key, $array2: value
$result = array();
for($i=0; $i<sizeof($array1); $i++)
$result[$array1[$i]] = $array2[i];

Categories