Remove duplicate values from an array [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to remove duplicate values from a multi-dimensional array in PHP
I would like to know how to remove the duplicate array values from the following
Array (
[0] => Array ( )
[1] => Array (
[0] => 7
[1] => 8
)
[2] => Array (
[0] => 7
)
[3] => Array (
[0] => 8
)
)

Use array_unique PHP function.
array_unique($variable);

Related

PHP Array Duplicate Value [duplicate]

This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 6 years ago.
I have an array like this:
Array
(
[0] => Array
(
[0] => Array
(
[Foo] => FooBar
)
)
[1] => Array
(
[0] => Array
(
[Foo] => BarFoo
)
)
[2] => Array
(
[0] => Array
(
[Foo] => FooFoo
)
)
[3] => Array
(
[0] => Array
(
[Foo] => FooFoo
)
)
)
I basically want to be able to look at [2] and [3] and be able to merge those two together since their values are the same. Basically I just don't want to double count. Is there a simple way of doing this while looping through?
You can try this
$unique_array = array_map("unserialize",
array_unique(array_map("serialize", $your_array)));

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 1 output from 2 output [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 7 years ago.
How to 1 output from 2 output ?
<?php
[a] => Array
(
[0] => 72.89.122.00:50827
[1] => 62.173.145.00:17211
);
[b] => Array
(
[0] => 61.10.231.00:52151
[1] => 66.171.81.00:41787
);
?>
I need output:
[total] => Array
(
[0] => 72.89.122.00:50827
[1] => 62.173.145.00:17211
[2] => 61.10.231.00:52151
[3] => 66.171.81.00:41787
);
Take a look at array_merge(); http://php.net/manual/en/function.array-merge.php
$newArray = array_merge($a,$b);
Use the array_merge function described here.

Sorting multidimensional array on inner value php [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
Say I have the following array, how can I sort it on sort_by?
Array
(
[10] => Array
(
[Masthead_slide] => Array
(
[id] => 1456464564
[sort_by] => 1
)
)
[6] => Array
(
[Masthead_slide] => Array
(
[id] => 645454
[sort_by] => 10
)
)
[7] => Array
(
[Masthead_slide] => Array
(
[id] => 4547
[sort_by] => 5
)
)
)
Try by this
function sortByOrder($a, $b) {
return $a['sort_by'] - $b['sort_by'];
}
usort($arr, 'sortByOrder');

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

Categories