PHP array unique does not work like expected [duplicate] - php

This question already has answers here:
How to remove duplicate values from an array in PHP
(26 answers)
Closed 1 year ago.
I have a problem with array_unique() in PHP.
Here is my code:
$filterGroupsArray = ['', 'a', 'a', 'b'];
print_r($filterGroupsArray);
array_unique($filterGroupsArray);
print_r($filterGroupsArray);
The output is
Array ( [0] => [1] => a [2] => a [3] => b ) Array ( [0] => [1] => a [2] => a [3] => b )
but I am expecting
Array ( [0] => [1] => a [2] => a [3] => b ) Array ( [0] => [1] => a [2] => b )
What did I do wrong?
Thank you very much!

array_unique returns a new array with the duplicate values removed.
$uniqueArray = array_unique($filterGroupsArray);
print_r($uniqueArray);

Related

PHP multi dimentional array sum and make unique [duplicate]

This question already has answers here:
Associative array, sum values of the same key
(5 answers)
Closed 3 years ago.
Following is my array. I need to add its Sum field according to each emp_firstname. Some has only one time coming, some coming two times. how can we sum the field and make the array unique?
Array
(
[0] => Array
(
[emp_firstname] => Alistair
[non_pm] => AMZ
[sum] => 2
)
[1] => Array
(
[emp_firstname] => Shakkeer
[non_pm] => SHK
[sum] => 3
)
[2] => Array
(
[emp_firstname] => Waqas
[non_pm] => WAS
[sum] => 12
)
[3] => Array
(
[emp_firstname] => Zain
[non_pm] => ZAI
[sum] => 9
)
[4] => Array
(
[emp_firstname] => Shakkeer
[gud_pmeditor] => SHK
[sum] => 4
)
[5] => Array
(
[emp_firstname] => Zain
[gud_pmeditor] => ZAI
[sum] => 2
)
)
You can get the desired result using this approach
$res=[];
foreach($arr as $val){
if(array_key_exists($val['emp_firstname'], $res))
$res[$val['emp_firstname']]['sum'] = ($res[$val['emp_firstname']]['sum'] + $val['sum']);
else
$res[$val['emp_firstname']] = $val;
}
Live Demo

How to remove duplicate values from arrays [duplicate]

This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 7 years ago.
I have these arrays i want to remove remove duplicate links from my array how can i do this please help
I have many links in my array first array have no key and others have key all links have unique ids i want to remove same id links and submit it into mysql. all work is done now i am stuck in this duplicate issue please kindly help me.
Array
(
[0] => mainlink
[apple] => Array
(
[0] => http://apple1.to/getac/fdjpkb9xdixq
[1] => http://apple1.to/getac/fdjpkb9xdixq
[2] => http://apple1.to/getac/fdjpkb9xdixq
[3] => http://apple2.to/getac/fdjpkb9xdixq
[4] => http://apple2.to/getac/fdjpkb9xdixq
[5] => http://apple2.to/getac/fdjpkb9xdixq
)
[banana] => Array
(
[0] => http://banana1.to/getac/fdjpkb9xdixq
[1] => http://banana2.to/getac/fdjpkb9xdixq
[2] => http://banana1.to/getac/fdjpkb9xdixq
[3] => http://banana2.to/getac/fdjpkb9xdixq
)
)
Thanks.
I want this result:
Array
(
[0] => mainlink
[apple] => Array
(
[0] => http://apple1.to/getac/fdjpkb9xdixq
[3] => http://apple2.to/getac/fdjpkb9xdixq
)
[banana] => Array
(
[0] => http://banana1.to/getac/fdjpkb9xdixq
[1] => http://banana2.to/getac/fdjpkb9xdixq
)
)
This should work for you:
Just loop through your array with array_map() and check if it is an array or not. If yes just return the unique array with array_unique(), else just return the value.
<?php
$unique = array_map(function($v){
if(is_array($v))
return array_unique($v);
return $v;
}, $array);
print_r($unique);
?>

how to change index of an array in php [duplicate]

This question already has answers here:
Built-in PHP function to reset the indexes of an array?
(4 answers)
Closed 8 years ago.
I want to change the index of an array after doing some operations
My actual output is
Array
(
[0] => Array
(
[0] => 4
[1] => 6
)
[2] => Array
(
[0] => 1
[1] => 7
)
[5] => Array
(
[0] => 1
[1] => 7
)
)
I want to be something like this
Array
(
[0] => Array
(
[0] => 4
[1] => 6
)
[1] => Array
(
[0] => 1
[1] => 7
)
[2] => Array
(
[0] => 1
[1] => 7
)
)
How to achieve this in php???? Thanks in advance
Extract only the values to a new array and the keys will be indexed from 0:
$array = array_values($array);
The array_values() function returns only values from array.
Thus, resetting all the array keys to numeric starting from 0, 1, 2, ...
You can do it using
$array = array_values($array);
http://php.net/manual/en/function.array-values.php
you can use the "array_values" Function for that.
Please refer this link http://www.php.net/manual/en/language.types.array.php

Change several ARRAY into new ARRAYS [duplicate]

This question already has answers here:
Combine arrays in PHP [duplicate]
(4 answers)
Closed 10 months ago.
I would like to know how to change the contents from several ARRAYS into new ARRAYS.
I have this 3 vars with a ARRAY each, lets say the first var is $number and it has this array:
Array
(
[0] => 1
[1] => 3
[2] => 9
)
The second var is $item and it has this:
Array
(
[0] => house
[1] => car
[2] => bike
)
And the third is $color and it has this:
Array
(
[0] => red
[1] => white
[2] => black
)
How can I change the contents and create new arrays like this:
Array
(
[0] => 1
[1] => house
[2] => red
)
Array
(
[0] => 3
[1] => car
[2] => white
)
Array
(
[0] => 9
[1] => bike
[2] => black
)
You can use array_map:
<?php
$number = [1,3,9];
$item = ['house','car','bike'];
$color = ['red','white','black'];
$res = array_map(null, $number, $item, $color);
print_r($res);
?>
which will output a single array of arrays that you want:
Array
(
[0] => Array
(
[0] => 1
[1] => house
[2] => red
)
[1] => Array
(
[0] => 3
[1] => car
[2] => white
)
[2] => Array
(
[0] => 9
[1] => bike
[2] => black
)
)
You can make a callback function with array_map() that returns each value together:
$result = array();
function merge_arrays($a,$b,$c){
return array($a,$b,$c);
}
$result = array_map("merge_arrays",$number,$item,$color);
DEMO

array merge issue in php [duplicate]

This question already has answers here:
Combine arrays in PHP [duplicate]
(4 answers)
Closed 10 months ago.
I want to merge array in php. I have array like this:
Array ( [0] => 6 [1] => 3 [2] => 15 )
Array ( [0] => IMAGE [1] => TICKER [2] => FULL_SCREEN_VIDEO )
Array ( [0] => 434 [1] => 423 [2] => 123 )
And I want result like:
Array( [0] => 6 [1] => IMAGE [2] => 434)
Array( [0] => 3 [1] => TICKER [2] => 423)
Array( [0] => 15 [1] => FULL_SCREEN_IMAGE [2] => 123)
What will be the easiest solution for this kind of problem?
Thanks..
You want to "transpose" an array. Assuming you have these 3 arrays in an array, you can do this:
$array = array(
array(6, 3, 15),
array('IMAGE', 'TICKER', 'FILL_SCREEN_VIDEO'),
array(434, 423, 123)
);
array_unshift($array, null);
$array = call_user_func_array("array_map", $array);
If your arrays are actually 3 separate arrays, then you could just simply do this:
$array = array_map(null, $array1, $array2, $array3);
That's basically what call_user_func_array is doing.

Categories