This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
How to remove duplicate values from an array in PHP
(27 answers)
Closed last year.
all
I want to remove the duplicate value from this Array
Array
(
[0] => Array
(
[0] => Ajay Patel
[1] => Tag 1
)
[1] => Array
(
[0] => Tag 1
[1] => Tag 3
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
)
I tried this solution from How to remove duplicate values from a multi-dimensional array in PHP
$result2 = array_map("unserialize", array_unique(array_map("serialize", $result2)));
But i think something is wrong here, i am getting this as result.
Array
(
[0] => Array
(
[0] => Ajay Patel
[1] => Tag 1
)
[1] => Array
(
[0] => Tag 1
[1] => Tag 3
)
[2] => Array
(
)
)
What i want is
Array
(
[0] => Ajay Patel
[1] => Tag 1
[2] => Tag 3
)
Tag 1 is removed because its 2 times...
$result2 = array_unique(call_user_func_array('array_merge',$result2));
In modern PHP, the same technique can be written as:
$result2 = array_unique(array_merge(...$result2));
try this
$result = array();
function merge_values(array &$array, $mixed) {
if(is_array($mixed)) {
foreach($mixed as $tags) {
merge_values($array, $tags);
}
}
else {
if(null !== $mixed && strlen($mixed) > 0 && false === array_search($mixed, $array)) {
$array[] = $mixed;
}
}
}
merge_values($result, $array);
print_r($result);
I think you should try this
function uniqueElements($outerArray){
$result=array();
foreach ($outerArray as $innerArray){
$result=array_merge($innerArray);
}
return array_unique($result);
}
Related
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 7 months ago.
If I have an array $array like this:
Array (
[0] => Array (
[id] => 11
[name] => scifi
)
[1] => Array (
[id] => 12
[name] => documetary
)
[2] => Array (
[id] => 10
[name] => comedy
)
)
How could I turn it into simply:
Array ( 11, 12, 10 ) with no key value pairs.
I am trying to extract only the id from each array and add them into 1 array. I am trying it with a foreach;
$ids = [];
if ( $array ) {
foreach ( $array as $item ) {
$ids[] = $term->id;
}
}
print_r($ids);
It just returns 3 empty arrays Array ( [0] => [1] => [2] => )
Using a loop, you can do this:
$arr1 = [
['id'=>11,'name'=>'scifi'],
['id'=>12,'name'=>'documentry'],
['id'=>10,'name'=>'comedy'],
];
$arr2 = [];
foreach($arr1 as $internal){
array_push($arr2,$internal['id']);
}
print_r($arr2);
Here we access all internal array's ID's and insert them into a new array.
This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Closed 5 years ago.
It's probably beginner question but I'm going through documentation for
longer time already and I can't find any solution and i have an array which
is multidimensional given below format.
/* This is how my array is currently */
Array
(
[0] => Array
(
[0] => Array
(
[title] => a
[title_num] =>1
[status] => 1
)
[1] => Array
(
[title] => Mr
[title_num] => 82
[status] => 1
)
)
[1] => Array
(
[0] => Array
(
[title] => b
[title_num] =>25
[status] => 2
)
[1] => Array
(
[title] => c
[title_num] =>45
[status] => 2
)
)
)
I want to convert this array into this form
/*Now, I want to simply it down to this*/
Array
(
[0] => Array
(
[title] => a
[title_num] =>1
[status] => 1
)
[1] => Array
(
[title] => Mr
[title_num] => 82
[status] => 1
)
[2] => Array
(
[title] => b
[title_num] =>25
[status] => 2
)
[3] => Array
(
[title] => c
[title_num] =>45
[status] => 2
)
)
I have tried array_flatten,array_map PHP built in function
A link or anything to point me in the right direction will be highly
appreciated
Here is another trick to solve your problem,
function custom_filter($array) {
$temp = [];
array_walk($array, function($item,$key) use (&$temp){
foreach($item as $value)
$temp[] = $value;
});
return $temp;
}
array_walk — Apply a user supplied function to every member of an array
Here is working demo.
here you go
$result = [];
foreach($array as $arr)
{
$result = array_merge($result , $arr);
}
var_dump($result);
Like this:
$i=0;
foreach ($array as $n1) {
foreach ($n1 as $n2) {
$newArr[$i]['title']=$n2['title'];
$newArr[$i]['title_num']=$n2['title_num'];
$newArr[$i]['status']=$n2['status'];
}
$i++;
}
Or simpler as suggested in the comments
for ($i=0; $i < count($array); $i++) {
foreach ($array[$i] as $n2) {
$newArr[$i]['title']=$n2['title'];
$newArr[$i]['title_num']=$n2['title_num'];
$newArr[$i]['status']=$n2['status'];
}
}
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I have array in following format:
Array
(
[sales] => Array
(
[0] => Array
(
[0] => 1
[1] => 6
)
[1] => Array
(
[0] => 2
[1] => 8
)
[2] => Array
(
[0] => 3
[1] => 25
)
[3] => Array
(
[0] => 4
[1] => 34
)
)
)
Using:
foreach ($data['sales'] as $k => $row) {
$list = implode(",",$row);
}
I get the following as output:
1,62,83,254,34
But I only need the second values from each subArray. The expected result needs to be:
6,8,25,34
How can I remove the first set of values?
Just grab the first column from your array with array_column(), so that you end up with an array, e.g.
Array (
[0] => 6
[1] => 8
[2] => 25
[3] => 34
)
And implode() it then as you already did, e.g.
echo implode(",", array_column($data["sales"], 1));
output:
6,8,25,34
I like array_column() but if you don't have PHP >= 5.5.0:
$list = implode(',', array_map(function($v) { return $v[1]; }, $data['sales']));
Or with the foreach:
foreach ($data['sales'] as $row) {
$list[] = $row[1];
}
$list = implode(',', $list);
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:
php remove duplicates from array
(5 answers)
Closed 9 years ago.
I have for example var $test and the output is below. You can see its duplicated.
How can i make it not duplicated? Is there any function for it?
Array (
[title] => Array (
[0] => this field is required
[1] => must be longer than2
)
[subtitle] => Array (
[0] => this field is required
[1] => must be longer than2
)
)
Array (
[title] => Array (
[0] => this field is required
[1] => must be longer than2
)
[subtitle] => Array (
[0] => this field is required
[1] => must be longer than2
)
)
Expected result:
Array (
[title] => Array (
[0] => this field is required
[1] => must be longer than2
)
[subtitle] => Array (
[0] => this field is required
[1] => must be longer than2
)
)
function intersect($data=NULL){
if(!empty($data)){$crashed = array();
$crashed2 = array();
foreach($data[0] as $key=>$val){
if(!is_array($val)){
$crashed[$key] = in_array($val,$data[1]);//return true if crashed(intersect)
}else{
$crashed2[$key] = intersect(array($val,$data[1]));
}
$crashed = array_merge($crashed,$crashed2);
}
}return $crashed;
}
$intersect =intersect(array($array1,$array2));
print_r($intersect);
You can use ;
$unique = array_map("unserialize", array_unique(array_map("serialize", $array)));
print_r($unique);
See Live Demo