I want to remove duplicate elements from an array in php. Following is the structure of the array
Array
(
[0] => Array
(
[0] => xllga#hotmail.com
[1] => bounce#abc.com
[2] => 20120416135504.21734.qmail#abc.com
[3] => xllga#hotmail.com
[4] => info#abc.com
[5] => info#abc.com
[6] => xllga#hotmail.com
[7] => xllga#hotmail.com
)
)
How to go about it ?
Try array_unique.
Code :
<?php
$arr = array_unique($arr);
?>
array array_unique ( array $array [, int $sort_flags = SORT_STRING
] )
Takes an input array and returns a new array without duplicate values.
Try array_unique():
$newArray = array_unique($oldArray);
From the docs:
[array_unique()] Takes an input array and returns a new array without duplicate values.
http://php.net/manual/en/function.array-unique.php
$new_unique_array = array_unique($your_array[0]);
Hope that helps,
Stefan
No array_unique() solution. not so smart:)
array_keys(array_flip($array));
If use your array, $array = $yourArray[0];
Related
I have an array-like:
Array
(
[0] => 4
[1] => 1861
)
Array
(
[0] => 4
[1] => 1938
)
Array
(
[0] => 4
[1] => 1452
)
Array
(
[0] => 4
[1] => 1938
)
Array
(
[0] => 21
)
This is a single array contain this array of elements.
I need the result like :
$arr = array(4,1861,1938,1452,21);
That means I need the only array unique values from these arrays. For this, I am using array_walk_recursive(), array_merge() etc. But I didn't get my results.
Merge all the arrays and then call array_unique().
$result = array_unique(array_merge(...$array));
...$array spreads the elements of the original array into arguments to array_merge().
If you're using an old version of PHP before ellipsis, use call_user_func_array() instead.
$result = array_unique(call_user_func_array('array_merge', $array));
you need to use array_merge then array_unique
$array = array_unique (array_merge ($array1, $array2));
check this please https://stackoverflow.com/a/10572576/2429434
I want to search and delete $srch_data data from the $list, but array_search() is not working. What's going wrong?
$srch_data = 'neha,neha#xyz.com';
$list = "gaurav,gaurav#xyz.com,neha,neha#xyz.com,ayush,ayush#xyz.com";
$arr = explode(',',$list);
$list_array = array_chunk($arr,2);
$pos = array_search($srch_data,$list_array);
echo $pos;
The problem is that you are searching with different things. Once you have used array_chunk(), your data is...
Array
(
[0] => Array
(
[0] => gaurav
[1] => gaurav#xyz.com
)
[1] => Array
(
[0] => neha
[1] => neha#xyz.com
)
...
and you are searching for
'neha,neha#xyz.com'
so this will not match.
If you converted your search string to an array as well, this will work...
$pos = array_search(explode(",", $srch_data),$list_array);
I have an array with following format.
Array
(
[0] => Array
(
[keyword] => thumbnail_follow
[content] => width
)
[1] => Array
(
[keyword] => thumbnail_resize
[content] => yes
)
)
My desire output is following
Array
(
[thumbnail_follow] => width
[thumbnail_resize] => yes
)
What I have tried so far?
array_shift() but this remove duplicate keys and output become
Array
(
[keyword] => thumbnail_follow
[content] => width
)
array_column I am able to get only single type of values. For example array_column($array, 'keyword')
Array
(
[0] => thumbnail_follow
[1] => thumbnail_resize
)
Using array_column and loop, I can get desire output but there must be some more efficient way to do it but I am known from this
Can you please help me guys?
Using array_column 's third parameter index_key you can do it. Please look at 2nd example of function.
Use it like this
print_r( array_column($array, 'content', 'keyword') );
You should get your desire output by this.
Try this
$newArr = [];
foreach($yourArr as $row) {
$newArr[$row['keyword']] = $row['content'];
}
My first array is like:
Array ( [0] => Array ( [column_name] => ben_firstname ) )
Second array:
Array ( [0] => Array ( [column_name] => ben_unique_id )
[1] => Array ( [column_name] => ben_firstname )
[2] => Array ( [column_name] => ben_lastname )
[3] => Array ( [column_name] => ben_middlename ) )
I want to remove ben_firstname (which is in first array) from second array...
I tried with array_diff function. But, I am getting error.
CODE:
print_r(array_diff($first_array, $second_array));
ERROR:
Message: Array to string conversion
Thanks for your help.
You can't use array_diff directly because that function expects array elements to be scalar, while in your case they are themselves arrays.
The correct solution is to use array_udiff with a callback that determines equality by looking at the column_name key of each array:
$result = array_udiff(
$second,
$first,
function($x, $y) { return strcmp($x['column_name'], $y['column_name']); }
);
See it in action.
You need to do:
$newArray = array_diff($array2, $array1);
The order is important. What error are you getting?
EDIT: Your error is that you're trying to print an array, not that the array_diff didn't work.
If you want to print a PHP array, print_r should work. What PHP version are you on?
Is it possible to convert the following array using some PHP built-in functions to a array that contain the value of id as key and the value of label as the associated value? If not what's the efficient way?
Thanks.
Input Array:
Array
(
[0] => Array
(
[id] => 2
[label] => MTD-589
)
[1] => Array
(
[id] => 3
[label] => MTD-789
)
)
Output Array:
Array
(
[2] => MTD-589,
[3] => MTD-789,
)
I don't know any built in function, but I'll do it this way:
assuming $originalArray as the array you want to convert
$newArray = array();
foreach ($originalArray as $element)
$newArray[$element["id"]] = $element["label"];
output the result
var_dump($newArray);
Introducing array_column (still in PHP 5.5 Beta).
$new_array = array_column($your_array 'label', 'id');
OUTPUT:
Array
(
[2] => MTD-589,
[3] => MTD-789,
)
Using array_walk.
array_walk($array, function($a) use (&$return) { $return[$a['id']] = $a['label']; });
print_r($return);
if you can make it so that one array has your IDs and one array has your labels, you can use array_combine to merge the two as key/value http://php.net/manual/en/function.array-combine.php