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);
?>
Related
This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 2 years ago.
I searched for solutions on here but didn't find one for my use case.
I have a big array which is built like this example:
Array
(
[0] => Array
(
[Template] => page.html5
)
[1] => Array
(
[Template] => page2.html5
)
[2] => Array
(
[Template] => page.html5
)
[3] => Array
(
[Template] => page2.html5
)
[4] => Array
(
[Template] => page.html5
)
[5] => Array
(
[Template] => page2.html5
)
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
I would like to remove all duplicate values for the array key "Template", but besides that I want the array to stay the way it is.
So afterwards my Array should look like:
Array
(
[0] => Array
(
[Template] => page.html5
)
[1] => Array
(
[Template] => page2.html5
)
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
Is there a way to achieve this without using lots of memory?
Thanks for your answers :)
You could use the following logic, which uses:
array_map() to flatten the array with index keys-values, and serialize() (stringify) the last array element so we can use
array_unique() on the result.
Then, to restore the stringified array, i.e. turn it back into an array, we use unserialize().
<?php
$newArr = array_unique(array_map(function ($el) {
return $el['Template'] ?? serialize($el);
}, $arr));
// restore the last element to array
$last = array_key_last($newArr); // (PHP 7 >= 7.3.0)*
$newArr[$last] = unserialize($newArr[$last]);
*if PHP version <7.3.0 use: end($newArr); $last = key($newArr);
Output:
Array
(
[0] => page.html5
[1] => page2.html5
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
working demo
The code below loops the array, marks indexes for removal and then another loop does the removals:
$templates = array(); //This will store the remove plan
for ($index = 0; $index < count($input); $index++) {
if (isset($input[$index]["Template"])) { //Ignore items where there is no template
if (isset($templates[$input[$index]["Template"]])) { //Let's check whether we have already seen this template
$templates[$input[$index]["Template"]] = array(); //From now on we will find duplicates for this dude
} else { //Mark for removal
$templates[$input[$index]["Template"]][]=$index;
}
}
}
//Actual removals
foreach($templates => $index) {
//Removing the actual element:
unset($input[$index]["Template"]);
//Remove the parent as well if it becomes empty
if (!count($input[$index])) unset($input[$index]);
}
The memory need for this algorithm is:
average(element_size) * number_of_elements
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)));
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
This question already has answers here:
Push elements from one array into rows of another array (one element per row)
(4 answers)
Closed 8 years ago.
Hi i have an array like this
Array
(
[0] => Array
(
[employeename] => abc
)
[1] => Array
(
[employeename] => def
)
)
Array
(
[0] => 1
[1] => 3
)
I need the second array value to be set in first array like this
Array
(
[0] => Array
(
[employeename] => abc
[othername] => 1
)
[1] => Array
(
[employeename] => def
[othername] => 3
)
)
Any Help Will be appreciated , Thanks In Advance :)
Try this :
<?php
$array1 = array(array("employeename" => "abc"),
array("employeename" => "def")
);
$array2 = array(1,3);
foreach($array1 as $key=>$val){
$array1[$key]["othername"] = $array2[$key];
}
echo "<pre>";
print_r($array1);
?>
This question already has answers here:
How do you reindex an array in PHP but with indexes starting from 1?
(12 answers)
How to re-index all subarray elements of a multidimensional array?
(6 answers)
Closed 10 months ago.
How do I run a array_map on a triple dimensional array? Where I want to "clear" the innermost array?
It looks like this:
Array
(
[1] => Array
(
[1] => Array
(
[cat] => Hello!
[url] => hello
)
[5] => Array
(
[cat] => Good job!
[url] => good-job
)
[2] => Array
(
[2] => Array
(
[cat] => How are you?
[url] => how-are-you
)
[6] => Array
(
[cat] => Running shoes
[url] => running-shoes
)
)
)
I want to make it look like this:
Array
(
[1] => Array
(
[1] => Array
(
[cat] => Hello!
[url] => hello
)
[2] => Array
(
[cat] => Good job!
[url] => good-job
)
[2] => Array
(
[1] => Array
(
[cat] => How are you?
[url] => how-are-you
)
[2] => Array
(
[cat] => Running shoes
[url] => running-shoes
)
)
)
This solution Reset keys of array elements in php? "just" works on tow diemensional arrays, if Im not wrong.
you could write a short function to do it with array_map:
function mappingfunction($array){
$remappedarray = array();
foreach($array as $layer){
$remappedarray[] = array_map('array_values', $array);
}
return $remappedarray;
}
if you want to preserve the keys:
function mappingfunction($array){
$remappedarray = array();
foreach($array as $key => $layer){
$remappedarray[$key] = array_map('array_values', $array);
}
return $remappedarray;
}
Untested, but should point you in the right direction.